2

I am writing a clang tool, yet I am quite new to it, so i came across a problem, that I couldn't find in the docs (yet).

I am using the great Matchers API to find some nodes that I will later want to manipulate in the AST. The problem is, that the clang tool will actually parse eeeverything that belongs to the sourcefile including headers like iostream etc. Since my manipulation will probably include some refactoring I definitely do not want to touch each and every thing the parser finds.

Right now I am dealing with this by comparing the sourceFiles of nodes that I matched against with the argumets in argv, but needless to say, that this feels wrong since it still parses through ALL the iostream code - it just ignores it whilst doing so. I just cant believe there is not a way to just tell the ClangTool something like:

"only match nodes which location's source file is something the user fed to this tool"

Thinking about it it only makes sense if its possible to individually create ASTs for each source file, but I do need them to be aware of each other or share contextual knowledge and I also haven't figured out a way to do that either.

I feel like I am missing something very obvious here.

thanks in advance :)

Julian
  • 525
  • 5
  • 19

1 Answers1

2

There are several narrowing matchers that might help: isExpansionInMainFile and isExpansionInSystemHeader. For example, one could combine the latter with unless to limit matches to AST nodes that are not in system files.

There are several examples of using these in the Code Analysis and Refactoring with Clang Tools repository. For example, see the file lib/callsite_expander.h around line 34, where unless(isExpansionInSystemHeader)) is used to exclude call expressions that are in system headers. Another example is at line 27 of lib/function_signature_expander.h, where the same is used to exclude function declarations in system headers that would otherwise match.

  • This still would not prevent the parsing of e.g. system headers tho, right? – Julian Apr 28 '18 at 03:31
  • That's correct--the system headers are included by the preprocessor as usual, then the whole translation unit is compiled to AST. It just means that AST nodes that originate in the system headers would be excluded. – Some Who Call Me Tim May 01 '18 at 05:01