[cfe-dev] [LibClang - C - AST] Switch-Case Statement Children

Jacob Faibussowitsch via cfe-dev cfe-dev at lists.llvm.org
Wed May 12 11:20:46 PDT 2021


Hello Clang Community,

Why is the first statement after a “case” label the child of the case label, but every other statement (including the break) after that its sibling? It seems like either all should be children or all should be siblings.

Suppose you have the following code snippet (full source files for any snippets below are included with this mail).

switch(expr) {
  case 1:
    func1();
    func2();
    break;
  case 2:
    func1();
    break;
}

Which when fed to clang with 

$ clang -Xclang -ast-dump -fsyntax-only test.c

produces the following AST  (edited for clarity):

| SwitchStmt <line:7:3, line:15:3>
    | `-CompoundStmt <col:15, line:15:3>
    |   |-CaseStmt <line:8:3, line:9:11>
    |   | `-CallExpr <line:9:5, col:11> —————————— func1() here is child to case 1
    |   |-CallExpr <line:10:5, col:11> —————————— func2() here is sibling to case 1
    |   |-BreakStmt <line:11:5>
    |   |-CaseStmt <line:12:3, line:13:11>
    |   | `-CallExpr <line:13:5, col:11> ————————— func1() here again is child to case 2
    |   `-BreakStmt <line:14:5>

My clang version and install info:

Apple clang version 12.0.5 (clang-1205.0.22.9)
Target: x86_64-apple-darwin20.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Best regards,

Jacob Faibussowitsch
(Jacob Fai - booss - oh - vitch)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20210512/dfe3d815/attachment-0004.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.c
Type: application/octet-stream
Size: 194 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20210512/dfe3d815/attachment-0002.obj>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20210512/dfe3d815/attachment-0005.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: astDump.out
Type: application/octet-stream
Size: 6115 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20210512/dfe3d815/attachment-0003.obj>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20210512/dfe3d815/attachment-0006.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: astDumpNoColor.txt
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20210512/dfe3d815/attachment-0001.txt>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20210512/dfe3d815/attachment-0007.html>


More information about the cfe-dev mailing list