TokenBuffer does not collect macro expansions inside macro arguments which is needed for semantic higlighting. Therefore collects macro expansions in the main file in a PPCallback when building the ParsedAST instead.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
- Build Status
Buildable 37485 Build 37484: arc lint + arc unit
Event Timeline
Looks great, just requests for comments and more test-cases from my side
clang-tools-extra/clangd/ClangdUnit.h | ||
---|---|---|
148 | NIT: clarify that this is the start location of the identifier that was macro-expanded. | |
149 | Could you please document whether these include:
| |
clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp | ||
248 | Could you add a few more interesting cases?
// foo.inc int a = ID(1); // foo.cpp #define ID(X) X int b; #include "foo.inc"
#define FOO(X) X##1() #define MACRO1() 123 int a = FOO(MACRO);
#define FOO BAR #define BAR 1 int a = FOO; // should BAR at line 1 be highlighted?
#define FOO 1 // Preamble ends here. int a = 10; #include "some_file_with_macros.h" // <-- should not get any macros from here | |
264 | NIT: maybe use push_back and for-each loop, the resulting code should be simpler and readability is more important for the test code than performance: std::vector<Position> Positions; for (SourceLocation Loc : AST.getMainFileMacros()) Positions.push_back(sourceLocToPosition(Loc)); |
Clarified comments. Added tests. Not getting expansions inside other macro expansions.
clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp | ||
---|---|---|
248 | Does clangd handle .inc files differently from .h? Because if it doesn't shouldn't ` case 1 cover case 4 as well ? |
clang-tools-extra/clangd/ClangdUnit.cpp | ||
---|---|---|
107 | Maybe make this part of CollectMainFileMacros? It looks like a natural fit there and we won't need another instance of PPCallbacks. | |
120 | I believe getSpellingLoc is a no-op if isMacroID() is false. Maybe simplify to: if (!L.isMacroID() && isInsideMainFile(L, SM)) ... | |
clang-tools-extra/clangd/ClangdUnit.h | ||
119 | NIT: return llvm::ArrayRef<SourceLocation> instead of const vector | |
119 | NIT: maybe shorten the name to getMainFileExpansions? | |
150 | NIT: a comment like this or a similar one is probably also useful in the public accessor. | |
clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp | ||
248 | Right, sorry, was rushing to finish a comment. .inc and .h are handled exactly the same. | |
260 | Could we also test? #define PREPEND(X) Foo##X It'll probably be the same, but still interesting to see whether it's any differnet. |
clang-tools-extra/clangd/ClangdUnit.cpp | ||
---|---|---|
107 | But CollectMainFileMacros is only used for buildPreamble. Is it safe to add a CollectMainFileMacros in ParsedAST::build`? |
clang-tools-extra/clangd/ClangdUnit.cpp | ||
---|---|---|
107 | Ah, good point, LG then. Something like // CollectMainFileMacros and CollectMainFileMacroExpansions are two different classes because // the latter is only used when building preamble and the former only when building the AST for the main file. |
clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp | ||
---|---|---|
260 | So it turns out that the tests weren't actually passing before, must have accidentally forgot to save the file or something before compiling. Anyways tokens from concatenations are not included right now (which for highlighting is probably what we want, we don't highlight types/names that are from macro concatenations either) But the reason they are not are because:
Don't know how to fix any of this, and don't know if we actually want to collect these expansions either. |
LGTM, thanks!
clang-tools-extra/clangd/unittests/ClangdUnitTests.cpp | ||
---|---|---|
260 | It's definitely ok to not highlight those arguments that get concatenated. | |
268 | I've realized there's one last interesting case that we are missing: #define assert(COND) if (!(COND)) { printf("%s", #COND); exit(0); } void test(int a) { assert(0 <= a); // a should be highlighted } Could you please add this to the tests? If this works - great; if not - feel free to add a FIXME and land the patch as is, we can fix this later. |
NIT: return llvm::ArrayRef<SourceLocation> instead of const vector