0

I am trying to implement a clang tool that does syntactic analysis using ASTMatcher API. I am trying to find out how to specify extra flags for clang to disable semantic checks. I know clang builds a giant AST which includes system headers. Is there any way to parse source code while disabling semantic checks which give rise to unknown type errors? I just want to analyze the syntactic integrity of the source code of the given file. So far, I have tried to get around this problem by modifying the DSL to check whether the matching code is from the main file:

cxxRecordDecl(isExpansionInMainFile()).bind("class");

But this doesn't stop clang from looking into the header files.

gPats
  • 315
  • 2
  • 17

1 Answers1

0

Unfortunately, it is impossible to use plain syntactic analysis without sema. The problem is not specifically with clang, but with all of the parsers for C++ out there. Without simultaneous semantic analysis, any syntactic analysis is ambiguous. The issue is properly covered in this answer.

Valeriy Savchenko
  • 1,524
  • 8
  • 19
  • Thanks for clarifying! is there any way to generate the --fsyntax-only parse tree in ASTMatchers? – gPats Jan 28 '20 at 16:44
  • You can use `SyntaxOnlyAction`: https://clang.llvm.org/doxygen/classclang_1_1SyntaxOnlyAction.html#ae09b5c9647c100742c27ab2a90e956e5 Basically this (and --fsyntax-only as well) means that you get the AST (with semantic analysis) and don't go further than that (IR generation, optimization, and so on). – Valeriy Savchenko Jan 29 '20 at 07:52