llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.04k stars 11.98k forks source link

Clang Python marks assert as case statement #109787

Open Rubyfi opened 1 month ago

Rubyfi commented 1 month ago

Hi, I'm trying to parse a set of swtich-case statements using the Python clang module and ran into some weird behavior. Given something like this:

switch(Enum)
{
   case Enum::Value1:
      assert(false);
      break;
   case Enum::Value2:
      ....
      break;
....
}

Clang returns the assert statement's cursor kind as CursorKind.CASE_STMT. However, this only seems to happen when an assert is the first statement in a case. Any other statements parse like expected. When another expression is placed first in the case, the cursor of assert is CursorKind.COMPOUND_STMT instead:

switch(Enum)
{
   case Enum::Value1:
      ;
      assert(false);
      break;
   case Enum::Value2:
      ....
      break;
....
}

Am I misinterpreting the functionality of the underlying cursor; i.e. that it is supposed to help identifying the syntactical meaning of the according token; or is this a bug?

Rubyfi commented 1 month ago

Apologies, I might have posted this in the wrong repo. I'm using the libclang module (latest) to use the parser.

Apparently, this behaviour occurs with other kinds of macros as well:

#define MIN(a, b) ((a) < (b) ? (a) : (b))

...

switch(Enum)
{
   case Enum::Value1:
      MIN(1,2);
      break;
   case Enum::Value2:
      ....
      break;
...
}

and

#define ADD(a, b) (a + b)

...

switch(Enum)
{
   case Enum::Value1:
      ADD(1,2);
      break;
   case Enum::Value2:
      ....
      break;
...
}

both show the same problem.