Collect the macro definition locations for all the macros used in the main
file.
Details
- Reviewers
kadircet sammccall - Commits
- rGe3c6090e5976: [clangd] IncludeCleaner: Support macros
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
clang-tools-extra/clangd/IncludeCleaner.cpp | ||
---|---|---|
162 | can you add a trace for this and verify that it's not a signficant amount of time on a macro-heavy file (e.g. unittests?) locateMacroAt wasn't designed to be called in a loop like this, but it seems plausible it'll be fine | |
162 | iterating over all the tokens and examining them individually seems like we're doing too much work. ParsedAST.MainFileMacros.Names already knows the names of all macros referenced from the main file. This seems like it should be close enough, and wade through many fewer tokens? | |
166 | locateMacroAt doesn't check that the token is an identifier before trying to fetch its text, maybe do that? either here or there... | |
clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp | ||
128 | #define INNER 42 int answer = OUTER; (whatever the behavior is, let's add a test for it) | |
128 | #define ANSWER 42 int sq = SQUARE(ANSWER); |
Try another strategy to reduce the number of processed tokens (doesn't work in
all cases).
clang-tools-extra/clangd/IncludeCleaner.cpp | ||
---|---|---|
162 | Hmm, this doesn't really work for "unmaterialized" macros, i.e. ParsedAST::getMacros().Names doesn't have FOO in this case: // foo.h #define FOO // foo.cpp #define BAR FOO Is that a bug in CollectMainFileMacros we want to fix or is this intended behavior and we'd need to iterate through the tokens? |
Skip the tokens that are not identifiers or weren't defined as macros, attach
tracer.
Very nice. As discussed it'd be nice to enhance MainFileMacros at some point but it doesn't seem urgent
clang-tools-extra/clangd/IncludeCleaner.cpp | ||
---|---|---|
174 | This describes the implementation, not the purpose. Finds locations of all macros referenced from within the main file. This includes references that were not yet expanded, like `BAR` in `#define FOO BAR`. | |
181 | I'd rephrase a little:
| |
191 | this is just a fast-fail for non-identifiers, right? The hadMacroDefinition check is already done in locateMacroAt. Probably cleaner to move the == tok::identifier check inside locateMacroAt and remove the checks here. | |
clang-tools-extra/clangd/IncludeCleaner.h | ||
36–41 | The start of this sentence is hard to parse: "non-macros" as a noun is vague, and probably needs a comma after. "uses" and "go through" are a bit vague too. Maybe "A RecursiveASTVisitor finds references to symbols and records their associated locations. These may be macro expansions..." Really this is all describing the implementation though, and could be left out of the header entirely... | |
40 | something's happened to the punctuation here | |
clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp | ||
127 | Maybe PP-disabled too? "#define ^FOO\n" "#define ^BAR "#if 0 #if FOO BAR #endif #endif " |
something's happened to the punctuation here