diff --git a/clang/include/clang/Tooling/Syntax/Tokens.h b/clang/include/clang/Tooling/Syntax/Tokens.h --- a/clang/include/clang/Tooling/Syntax/Tokens.h +++ b/clang/include/clang/Tooling/Syntax/Tokens.h @@ -236,6 +236,15 @@ /// #pragma, etc. llvm::ArrayRef spelledTokens(FileID FID) const; + /// Get all tokens that expand a macro in FID. For the following input + /// #define FOO B + /// #define FOO2(X) int X + /// FOO2(XY) + /// int B; + /// FOO; + /// macroExpansions() returns {"FOO2", "FOO"}. + std::vector macroExpansions(FileID FID) const; + const SourceManager &sourceManager() const { return *SourceMgr; } std::string dumpForTests() const; diff --git a/clang/lib/Tooling/Syntax/Tokens.cpp b/clang/lib/Tooling/Syntax/Tokens.cpp --- a/clang/lib/Tooling/Syntax/Tokens.cpp +++ b/clang/lib/Tooling/Syntax/Tokens.cpp @@ -232,6 +232,21 @@ return E; } +std::vector +TokenBuffer::macroExpansions(FileID FID) const { + auto FileIt = Files.find(FID); + assert(FileIt != Files.end() && "file not tracked by token buffer"); + auto &File = FileIt->second; + std::vector Expansions; + auto &Spelled = File.SpelledTokens; + for (auto Mapping : File.Mappings) { + const syntax::Token *Token = &Spelled[Mapping.BeginSpelled]; + if (Token->kind() == tok::TokenKind::identifier) + Expansions.push_back(Token); + } + return Expansions; +} + std::vector syntax::tokenize(FileID FID, const SourceManager &SM, const LangOptions &LO) { std::vector Tokens; diff --git a/clang/unittests/Tooling/Syntax/TokensTest.cpp b/clang/unittests/Tooling/Syntax/TokensTest.cpp --- a/clang/unittests/Tooling/Syntax/TokensTest.cpp +++ b/clang/unittests/Tooling/Syntax/TokensTest.cpp @@ -59,7 +59,6 @@ using ::testing::Matcher; using ::testing::Not; using ::testing::StartsWith; - namespace { // Checks the passed ArrayRef has the same begin() and end() iterators as the // argument. @@ -755,4 +754,27 @@ // We don't test assertion failures because death tests are slow. } +TEST_F(TokenBufferTest, macroExpansions) { + llvm::Annotations Code(R"cpp( + #define FOO B + #define FOO2 BA + #define CALL(X) int X + #define G CALL(FOO2) + int B; + $macro[[FOO]]; + $macro[[CALL]](A); + $macro[[G]]; + )cpp"); + recordTokens(Code.code()); + auto &SM = *SourceMgr; + auto Expansions = Buffer.macroExpansions(SM.getMainFileID()); + std::vector ExpectedMacroRanges; + for (auto Range : Code.ranges("macro")) + ExpectedMacroRanges.push_back( + FileRange(SM.getMainFileID(), Range.Begin, Range.End)); + std::vector ActualMacroRanges; + for (auto Expansion : Expansions) + ActualMacroRanges.push_back(Expansion->range(SM)); + EXPECT_EQ(ExpectedMacroRanges, ActualMacroRanges); +} } // namespace