[PATCH] D147034: [clangd] Replace the hacky include-cleaner macro-reference implementation.
Haojian Wu via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 28 02:28:53 PDT 2023
hokein created this revision.
hokein added a reviewer: kadircet.
Herald added a subscriber: arphaman.
Herald added a project: All.
hokein requested review of this revision.
Herald added subscribers: MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.
Now MainFileMacros preserves enough information, we perform a just-in-time
convertion to interop with include-cleaner::Macro for include-cleaer features.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D147034
Files:
clang-tools-extra/clangd/IncludeCleaner.cpp
clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
Index: clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
+++ clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
@@ -189,6 +189,10 @@
#define DEF(X) const Foo *X;
#define BAZ(X) const X x
+// No missing include insertion for ambiguous macro refs.
+#if defined(FOO)
+#endif
+
void foo() {
$b[[b]]();
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===================================================================
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -136,22 +136,33 @@
}
std::vector<include_cleaner::SymbolReference>
-collectMacroReferences(ParsedAST &AST) {
+convertMacroReferences(ParsedAST &AST) {
const auto &SM = AST.getSourceManager();
- // FIXME: !!this is a hacky way to collect macro references.
- std::vector<include_cleaner::SymbolReference> Macros;
auto &PP = AST.getPreprocessor();
- for (const syntax::Token &Tok :
- AST.getTokens().spelledTokens(SM.getMainFileID())) {
- auto Macro = locateMacroAt(Tok, PP);
- if (!Macro)
- continue;
- if (auto DefLoc = Macro->Info->getDefinitionLoc(); DefLoc.isValid())
- Macros.push_back(
- {Tok.location(),
- include_cleaner::Macro{/*Name=*/PP.getIdentifierInfo(Tok.text(SM)),
- DefLoc},
- include_cleaner::RefType::Explicit});
+ std::vector<include_cleaner::SymbolReference> Macros;
+ for (const auto &MAndRefs : AST.getMacros().MacroRefs) {
+ for (const auto &Ref : MAndRefs.second) {
+ auto L = sourceLocationInMainFile(SM, Ref.Rng.start);
+ if (!L) {
+ llvm::consumeError(L.takeError());
+ continue;
+ }
+ if (const auto *Tok =
+ AST.getTokens().spelledTokenAt(*L)) {
+ auto Macro = locateMacroAt(*Tok, PP);
+ if (!Macro)
+ continue;
+
+ if (auto DefLoc = Macro->Info->getDefinitionLoc(); DefLoc.isValid())
+ Macros.push_back(
+ {Tok->location(),
+ include_cleaner::Macro{
+ /*Name=*/PP.getIdentifierInfo(Tok->text(SM)), DefLoc},
+ Ref.InConditionalDirective
+ ? include_cleaner::RefType::Implicit
+ : include_cleaner::RefType::Explicit});
+ }
+ }
}
return Macros;
}
@@ -360,7 +371,7 @@
const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID());
std::vector<include_cleaner::SymbolReference> Macros =
- collectMacroReferences(AST);
+ convertMacroReferences(AST);
std::vector<MissingIncludeDiagInfo> MissingIncludes;
llvm::DenseSet<IncludeStructure::HeaderID> Used;
trace::Span Tracer("include_cleaner::walkUsed");
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147034.508936.patch
Type: text/x-patch
Size: 2837 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230328/cd0adc05/attachment.bin>
More information about the cfe-commits
mailing list