clang-tools 22.0.0git
clang-include-fixer/IncludeFixer.cpp
Go to the documentation of this file.
1//===-- IncludeFixer.cpp - Include inserter based on sema callbacks -------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "IncludeFixer.h"
10#include "clang/Format/Format.h"
11#include "clang/Frontend/CompilerInstance.h"
12#include "clang/Lex/HeaderSearch.h"
13#include "clang/Lex/Preprocessor.h"
14#include "clang/Parse/ParseAST.h"
15#include "clang/Sema/Sema.h"
16#include "llvm/Support/Debug.h"
17#include "llvm/Support/raw_ostream.h"
18
19#define DEBUG_TYPE "clang-include-fixer"
20
21using namespace clang;
22
23namespace clang {
24namespace include_fixer {
25namespace {
26/// Manages the parse, gathers include suggestions.
27class Action : public clang::ASTFrontendAction {
28public:
29 explicit Action(SymbolIndexManager &SymbolIndexMgr, bool MinimizeIncludePaths)
30 : SemaSource(new IncludeFixerSemaSource(SymbolIndexMgr,
31 MinimizeIncludePaths,
32 /*GenerateDiagnostics=*/false)) {}
33
34 std::unique_ptr<clang::ASTConsumer>
35 CreateASTConsumer(clang::CompilerInstance &Compiler,
36 StringRef InFile) override {
37 SemaSource->setFilePath(InFile);
38 return std::make_unique<clang::ASTConsumer>();
39 }
40
41 void ExecuteAction() override {
42 clang::CompilerInstance *Compiler = &getCompilerInstance();
43 assert(!Compiler->hasSema() && "CI already has Sema");
44
45 // Set up our hooks into sema and parse the AST.
46 if (hasCodeCompletionSupport() &&
47 !Compiler->getFrontendOpts().CodeCompletionAt.FileName.empty())
48 Compiler->createCodeCompletionConsumer();
49
50 clang::CodeCompleteConsumer *CompletionConsumer = nullptr;
51 if (Compiler->hasCodeCompletionConsumer())
52 CompletionConsumer = &Compiler->getCodeCompletionConsumer();
53
54 Compiler->createSema(getTranslationUnitKind(), CompletionConsumer);
55 SemaSource->setCompilerInstance(Compiler);
56 Compiler->getSema().addExternalSource(SemaSource);
57
58 clang::ParseAST(Compiler->getSema(), Compiler->getFrontendOpts().ShowStats,
59 Compiler->getFrontendOpts().SkipFunctionBodies);
60 }
61
63 getIncludeFixerContext(const clang::SourceManager &SourceManager,
64 clang::HeaderSearch &HeaderSearch) const {
65 return SemaSource->getIncludeFixerContext(SourceManager, HeaderSearch,
66 SemaSource->getMatchedSymbols());
67 }
68
69private:
70 IntrusiveRefCntPtr<IncludeFixerSemaSource> SemaSource;
71};
72
73} // namespace
74
76 SymbolIndexManager &SymbolIndexMgr,
77 std::vector<IncludeFixerContext> &Contexts, StringRef StyleName,
78 bool MinimizeIncludePaths)
79 : SymbolIndexMgr(SymbolIndexMgr), Contexts(Contexts),
80 MinimizeIncludePaths(MinimizeIncludePaths) {}
81
83
85 std::shared_ptr<clang::CompilerInvocation> Invocation,
86 clang::FileManager *Files,
87 std::shared_ptr<clang::PCHContainerOperations> PCHContainerOps,
88 clang::DiagnosticConsumer *Diagnostics) {
89 assert(Invocation->getFrontendOpts().Inputs.size() == 1);
90
91 // Set up Clang.
92 CompilerInstance Compiler(std::move(Invocation), std::move(PCHContainerOps));
93 Compiler.setFileManager(Files);
94
95 // Create the compiler's actual diagnostics engine. We want to drop all
96 // diagnostics here.
97 Compiler.createDiagnostics(new clang::IgnoringDiagConsumer,
98 /*ShouldOwnClient=*/true);
99 Compiler.createSourceManager(*Files);
100
101 // We abort on fatal errors so don't let a large number of errors become
102 // fatal. A missing #include can cause thousands of errors.
103 Compiler.getDiagnostics().setErrorLimit(0);
104
105 // Run the parser, gather missing includes.
106 auto ScopedToolAction =
107 std::make_unique<Action>(SymbolIndexMgr, MinimizeIncludePaths);
108 Compiler.ExecuteAction(*ScopedToolAction);
109
110 Contexts.push_back(ScopedToolAction->getIncludeFixerContext(
111 Compiler.getSourceManager(),
112 Compiler.getPreprocessor().getHeaderSearchInfo()));
113
114 // Technically this should only return true if we're sure that we have a
115 // parseable file. We don't know that though. Only inform users of fatal
116 // errors.
117 return !Compiler.getDiagnostics().hasFatalErrorOccurred();
118}
119
120static bool addDiagnosticsForContext(TypoCorrection &Correction,
121 const IncludeFixerContext &Context,
122 StringRef Code, SourceLocation StartOfFile,
123 ASTContext &Ctx) {
125 Code, Context, format::getLLVMStyle(), /*AddQualifiers=*/false);
126 if (!Reps || Reps->size() != 1)
127 return false;
128
129 unsigned DiagID = Ctx.getDiagnostics().getCustomDiagID(
130 DiagnosticsEngine::Note, "Add '#include %0' to provide the missing "
131 "declaration [clang-include-fixer]");
132
133 // FIXME: Currently we only generate a diagnostic for the first header. Give
134 // the user choices.
135 const tooling::Replacement &Placed = *Reps->begin();
136
137 auto Begin = StartOfFile.getLocWithOffset(Placed.getOffset());
138 auto End = Begin.getLocWithOffset(std::max(0, (int)Placed.getLength() - 1));
139 PartialDiagnostic PD(DiagID, Ctx.getDiagAllocator());
140 PD << Context.getHeaderInfos().front().Header
141 << FixItHint::CreateReplacement(CharSourceRange::getCharRange(Begin, End),
142 Placed.getReplacementText());
143 Correction.addExtraDiagnostic(std::move(PD));
144 return true;
145}
146
147/// Callback for incomplete types. If we encounter a forward declaration we
148/// have the fully qualified name ready. Just query that.
150 clang::SourceLocation Loc, clang::QualType T) {
151 // Ignore spurious callbacks from SFINAE contexts.
152 if (CI->getSema().isSFINAEContext())
153 return false;
154
155 clang::ASTContext &context = CI->getASTContext();
156 std::string QueryString = QualType(T->getUnqualifiedDesugaredType(), 0)
157 .getAsString(context.getPrintingPolicy());
158 LLVM_DEBUG(llvm::dbgs() << "Query missing complete type '" << QueryString
159 << "'");
160 // Pass an empty range here since we don't add qualifier in this case.
161 std::vector<find_all_symbols::SymbolInfo> MatchedSymbols =
162 query(QueryString, "", tooling::Range());
163
164 if (!MatchedSymbols.empty() && GenerateDiagnostics) {
165 TypoCorrection Correction;
166 FileID FID = CI->getSourceManager().getFileID(Loc);
167 StringRef Code = CI->getSourceManager().getBufferData(FID);
168 SourceLocation StartOfFile =
169 CI->getSourceManager().getLocForStartOfFile(FID);
171 Correction,
172 getIncludeFixerContext(CI->getSourceManager(),
173 CI->getPreprocessor().getHeaderSearchInfo(),
174 MatchedSymbols),
175 Code, StartOfFile, CI->getASTContext());
176 for (const PartialDiagnostic &PD : Correction.getExtraDiagnostics())
177 CI->getSema().Diag(Loc, PD);
178 }
179 return true;
180}
181
182/// Callback for unknown identifiers. Try to piece together as much
183/// qualification as we can get and do a query.
185 const DeclarationNameInfo &Typo, int LookupKind, Scope *S, CXXScopeSpec *SS,
186 CorrectionCandidateCallback &CCC, DeclContext *MemberContext,
187 bool EnteringContext, const ObjCObjectPointerType *OPT) {
188 // Ignore spurious callbacks from SFINAE contexts.
189 if (CI->getSema().isSFINAEContext())
190 return clang::TypoCorrection();
191
192 // We currently ignore the unidentified symbol which is not from the
193 // main file.
194 //
195 // However, this is not always true due to templates in a non-self contained
196 // header, consider the case:
197 //
198 // // header.h
199 // template <typename T>
200 // class Foo {
201 // T t;
202 // };
203 //
204 // // test.cc
205 // // We need to add <bar.h> in test.cc instead of header.h.
206 // class Bar;
207 // Foo<Bar> foo;
208 //
209 // FIXME: Add the missing header to the header file where the symbol comes
210 // from.
211 if (!CI->getSourceManager().isWrittenInMainFile(Typo.getLoc()))
212 return clang::TypoCorrection();
213
214 std::string TypoScopeString;
215 if (S) {
216 // FIXME: Currently we only use namespace contexts. Use other context
217 // types for query.
218 for (const auto *Context = S->getEntity(); Context;
219 Context = Context->getParent()) {
220 if (const auto *ND = dyn_cast<NamespaceDecl>(Context)) {
221 if (!ND->getName().empty())
222 TypoScopeString = ND->getNameAsString() + "::" + TypoScopeString;
223 }
224 }
225 }
226
227 auto ExtendNestedNameSpecifier = [this](CharSourceRange Range) {
228 StringRef Source =
229 Lexer::getSourceText(Range, CI->getSourceManager(), CI->getLangOpts());
230
231 // Skip forward until we find a character that's neither identifier nor
232 // colon. This is a bit of a hack around the fact that we will only get a
233 // single callback for a long nested name if a part of the beginning is
234 // unknown. For example:
235 //
236 // llvm::sys::path::parent_path(...)
237 // ^~~~ ^~~
238 // known
239 // ^~~~
240 // unknown, last callback
241 // ^~~~~~~~~~~
242 // no callback
243 //
244 // With the extension we get the full nested name specifier including
245 // parent_path.
246 // FIXME: Don't rely on source text.
247 const char *End = Source.end();
248 while (isAsciiIdentifierContinue(*End) || *End == ':')
249 ++End;
250
251 return std::string(Source.begin(), End);
252 };
253
254 /// If we have a scope specification, use that to get more precise results.
255 std::string QueryString;
256 tooling::Range SymbolRange;
257 const auto &SM = CI->getSourceManager();
258 auto CreateToolingRange = [&QueryString, &SM](SourceLocation BeginLoc) {
259 return tooling::Range(SM.getDecomposedLoc(BeginLoc).second,
260 QueryString.size());
261 };
262 if (SS && SS->getRange().isValid()) {
263 auto Range = CharSourceRange::getTokenRange(SS->getRange().getBegin(),
264 Typo.getLoc());
265
266 QueryString = ExtendNestedNameSpecifier(Range);
267 SymbolRange = CreateToolingRange(Range.getBegin());
268 } else if (Typo.getName().isIdentifier() && !Typo.getLoc().isMacroID()) {
269 auto Range =
270 CharSourceRange::getTokenRange(Typo.getBeginLoc(), Typo.getEndLoc());
271
272 QueryString = ExtendNestedNameSpecifier(Range);
273 SymbolRange = CreateToolingRange(Range.getBegin());
274 } else {
275 QueryString = Typo.getAsString();
276 SymbolRange = CreateToolingRange(Typo.getLoc());
277 }
278
279 LLVM_DEBUG(llvm::dbgs() << "TypoScopeQualifiers: " << TypoScopeString
280 << "\n");
281 std::vector<find_all_symbols::SymbolInfo> MatchedSymbols =
282 query(QueryString, TypoScopeString, SymbolRange);
283
284 if (!MatchedSymbols.empty() && GenerateDiagnostics) {
285 TypoCorrection Correction(Typo.getName());
286 Correction.setCorrectionRange(SS, Typo);
287 FileID FID = SM.getFileID(Typo.getLoc());
288 StringRef Code = SM.getBufferData(FID);
289 SourceLocation StartOfFile = SM.getLocForStartOfFile(FID);
291 Correction, getIncludeFixerContext(
292 SM, CI->getPreprocessor().getHeaderSearchInfo(),
293 MatchedSymbols),
294 Code, StartOfFile, CI->getASTContext()))
295 return Correction;
296 }
297 return TypoCorrection();
298}
299
300/// Get the minimal include for a given path.
302 StringRef Include, const clang::SourceManager &SourceManager,
303 clang::HeaderSearch &HeaderSearch) const {
304 if (!MinimizeIncludePaths)
305 return std::string(Include);
306
307 // Get the FileEntry for the include.
308 StringRef StrippedInclude = Include.trim("\"<>");
309 auto Entry =
310 SourceManager.getFileManager().getOptionalFileRef(StrippedInclude);
311
312 // If the file doesn't exist return the path from the database.
313 // FIXME: This should never happen.
314 if (!Entry)
315 return std::string(Include);
316
317 bool IsAngled = false;
318 std::string Suggestion =
319 HeaderSearch.suggestPathToFileForDiagnostics(*Entry, "", &IsAngled);
320
321 return IsAngled ? '<' + Suggestion + '>' : '"' + Suggestion + '"';
322}
323
324/// Get the include fixer context for the queried symbol.
326 const clang::SourceManager &SourceManager,
327 clang::HeaderSearch &HeaderSearch,
328 ArrayRef<find_all_symbols::SymbolInfo> MatchedSymbols) const {
329 std::vector<find_all_symbols::SymbolInfo> SymbolCandidates;
330 for (const auto &Symbol : MatchedSymbols) {
331 std::string FilePath = Symbol.getFilePath().str();
332 std::string MinimizedFilePath = minimizeInclude(
333 ((FilePath[0] == '"' || FilePath[0] == '<') ? FilePath
334 : "\"" + FilePath + "\""),
335 SourceManager, HeaderSearch);
336 SymbolCandidates.emplace_back(Symbol.getName(), Symbol.getSymbolKind(),
337 MinimizedFilePath, Symbol.getContexts());
338 }
339 return IncludeFixerContext(FilePath, QuerySymbolInfos, SymbolCandidates);
340}
341
342std::vector<find_all_symbols::SymbolInfo>
343IncludeFixerSemaSource::query(StringRef Query, StringRef ScopedQualifiers,
344 tooling::Range Range) {
345 assert(!Query.empty() && "Empty query!");
346
347 // Save all instances of an unidentified symbol.
348 //
349 // We use conservative behavior for detecting the same unidentified symbol
350 // here. The symbols which have the same ScopedQualifier and RawIdentifier
351 // are considered equal. So that clang-include-fixer avoids false positives,
352 // and always adds missing qualifiers to correct symbols.
353 if (!GenerateDiagnostics && !QuerySymbolInfos.empty()) {
354 if (ScopedQualifiers == QuerySymbolInfos.front().ScopedQualifiers &&
355 Query == QuerySymbolInfos.front().RawIdentifier) {
356 QuerySymbolInfos.push_back(
357 {Query.str(), std::string(ScopedQualifiers), Range});
358 }
359 return {};
360 }
361
362 LLVM_DEBUG(llvm::dbgs() << "Looking up '" << Query << "' at ");
363 LLVM_DEBUG(CI->getSourceManager()
364 .getLocForStartOfFile(CI->getSourceManager().getMainFileID())
365 .getLocWithOffset(Range.getOffset())
366 .print(llvm::dbgs(), CI->getSourceManager()));
367 LLVM_DEBUG(llvm::dbgs() << " ...");
368 llvm::StringRef FileName = CI->getSourceManager().getFilename(
369 CI->getSourceManager().getLocForStartOfFile(
370 CI->getSourceManager().getMainFileID()));
371
372 QuerySymbolInfos.push_back(
373 {Query.str(), std::string(ScopedQualifiers), Range});
374
375 // Query the symbol based on C++ name Lookup rules.
376 // Firstly, lookup the identifier with scoped namespace contexts;
377 // If that fails, falls back to look up the identifier directly.
378 //
379 // For example:
380 //
381 // namespace a {
382 // b::foo f;
383 // }
384 //
385 // 1. lookup a::b::foo.
386 // 2. lookup b::foo.
387 std::string QueryString = ScopedQualifiers.str() + Query.str();
388 // It's unsafe to do nested search for the identifier with scoped namespace
389 // context, it might treat the identifier as a nested class of the scoped
390 // namespace.
391 std::vector<find_all_symbols::SymbolInfo> MatchedSymbols =
392 SymbolIndexMgr.search(QueryString, /*IsNestedSearch=*/false, FileName);
393 if (MatchedSymbols.empty())
394 MatchedSymbols =
395 SymbolIndexMgr.search(Query, /*IsNestedSearch=*/true, FileName);
396 LLVM_DEBUG(llvm::dbgs() << "Having found " << MatchedSymbols.size()
397 << " symbols\n");
398 // We store a copy of MatchedSymbols in a place where it's globally reachable.
399 // This is used by the standalone version of the tool.
400 this->MatchedSymbols = MatchedSymbols;
401 return MatchedSymbols;
402}
403
404llvm::Expected<tooling::Replacements> createIncludeFixerReplacements(
405 StringRef Code, const IncludeFixerContext &Context,
406 const clang::format::FormatStyle &Style, bool AddQualifiers) {
407 if (Context.getHeaderInfos().empty())
408 return tooling::Replacements();
409 StringRef FilePath = Context.getFilePath();
410 std::string IncludeName =
411 "#include " + Context.getHeaderInfos().front().Header + "\n";
412 // Create replacements for the new header.
413 clang::tooling::Replacements Insertions;
414 auto Err =
415 Insertions.add(tooling::Replacement(FilePath, UINT_MAX, 0, IncludeName));
416 if (Err)
417 return std::move(Err);
418
419 auto CleanReplaces = cleanupAroundReplacements(Code, Insertions, Style);
420 if (!CleanReplaces)
421 return CleanReplaces;
422
423 auto Replaces = std::move(*CleanReplaces);
424 if (AddQualifiers) {
425 for (const auto &Info : Context.getQuerySymbolInfos()) {
426 // Ignore the empty range.
427 if (Info.Range.getLength() > 0) {
428 auto R = tooling::Replacement(
429 {FilePath, Info.Range.getOffset(), Info.Range.getLength(),
430 Context.getHeaderInfos().front().QualifiedName});
431 auto Err = Replaces.add(R);
432 if (Err) {
433 llvm::consumeError(std::move(Err));
434 R = tooling::Replacement(
435 R.getFilePath(), Replaces.getShiftedCodePosition(R.getOffset()),
436 R.getLength(), R.getReplacementText());
437 Replaces = Replaces.merge(tooling::Replacements(R));
438 }
439 }
440 }
441 }
442 return formatReplacements(Code, Replaces, Style);
443}
444
445} // namespace include_fixer
446} // namespace clang
A context for a file being processed.
bool runInvocation(std::shared_ptr< clang::CompilerInvocation > Invocation, clang::FileManager *Files, std::shared_ptr< clang::PCHContainerOperations > PCHContainerOps, clang::DiagnosticConsumer *Diagnostics) override
IncludeFixerActionFactory(SymbolIndexManager &SymbolIndexMgr, std::vector< IncludeFixerContext > &Contexts, StringRef StyleName, bool MinimizeIncludePaths=true)
A context for a file being processed.
const std::vector< HeaderInfo > & getHeaderInfos() const
Get header information.
StringRef getFilePath() const
Get the file path to the file being processed.
const std::vector< QuerySymbolInfo > & getQuerySymbolInfos() const
Get information of symbols being querid.
IncludeFixerContext getIncludeFixerContext(const clang::SourceManager &SourceManager, clang::HeaderSearch &HeaderSearch, ArrayRef< find_all_symbols::SymbolInfo > MatchedSymbols) const
Get the include fixer context for the queried symbol.
std::string minimizeInclude(StringRef Include, const clang::SourceManager &SourceManager, clang::HeaderSearch &HeaderSearch) const
Get the minimal include for a given path.
clang::TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo, int LookupKind, Scope *S, CXXScopeSpec *SS, CorrectionCandidateCallback &CCC, DeclContext *MemberContext, bool EnteringContext, const ObjCObjectPointerType *OPT) override
Callback for unknown identifiers.
bool MaybeDiagnoseMissingCompleteType(clang::SourceLocation Loc, clang::QualType T) override
Callback for incomplete types.
This class provides an interface for finding the header files corresponding to an identifier in the s...
static bool addDiagnosticsForContext(TypoCorrection &Correction, const IncludeFixerContext &Context, StringRef Code, SourceLocation StartOfFile, ASTContext &Ctx)
llvm::Expected< tooling::Replacements > createIncludeFixerReplacements(StringRef Code, const IncludeFixerContext &Context, const clang::format::FormatStyle &Style, bool AddQualifiers)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//