[clangd] Replace the hacky include-cleaner macro-reference implementation.

Now MainFileMacros preserves enough information, we perform a just-in-time
convertion to interop with include-cleaner::Macro for include-cleaer features.

Differential Revision: https://reviews.llvm.org/D147034
This commit is contained in:
Haojian Wu 2023-06-23 14:07:06 +02:00
parent 407ff50eca
commit 6585dd3b83
2 changed files with 23 additions and 10 deletions

View File

@ -353,20 +353,29 @@ getUnused(ParsedAST &AST,
std::vector<include_cleaner::SymbolReference>
collectMacroReferences(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())
std::vector<include_cleaner::SymbolReference> Macros;
for (const auto &[_, Refs] : AST.getMacros().MacroRefs) {
for (const auto &Ref : Refs) {
auto Loc = SM.getComposedLoc(SM.getMainFileID(), Ref.StartOffset);
const auto *Tok = AST.getTokens().spelledTokenAt(Loc);
if (!Tok)
continue;
auto Macro = locateMacroAt(*Tok, PP);
if (!Macro)
continue;
auto DefLoc = Macro->NameLoc;
if (!DefLoc.isValid())
continue;
Macros.push_back(
{include_cleaner::Macro{/*Name=*/PP.getIdentifierInfo(Tok.text(SM)),
{include_cleaner::Macro{/*Name=*/PP.getIdentifierInfo(Tok->text(SM)),
DefLoc},
Tok.location(), include_cleaner::RefType::Explicit});
Tok->location(),
Ref.InConditionalDirective ? include_cleaner::RefType::Ambiguous
: include_cleaner::RefType::Explicit});
}
}
return Macros;
}

View File

@ -186,6 +186,10 @@ $insert_f[[]]$insert_vector[[]]
#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]]();