| 1 | //===--- ParsedAST.cpp -------------------------------------------*- C++-*-===// |
| 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 "ParsedAST.h" |
| 10 | #include "../clang-tidy/ClangTidyCheck.h" |
| 11 | #include "../clang-tidy/ClangTidyDiagnosticConsumer.h" |
| 12 | #include "../clang-tidy/ClangTidyModuleRegistry.h" |
| 13 | #include "AST.h" |
| 14 | #include "Compiler.h" |
| 15 | #include "Config.h" |
| 16 | #include "Diagnostics.h" |
| 17 | #include "Feature.h" |
| 18 | #include "FeatureModule.h" |
| 19 | #include "Headers.h" |
| 20 | #include "HeuristicResolver.h" |
| 21 | #include "IncludeCleaner.h" |
| 22 | #include "IncludeFixer.h" |
| 23 | #include "Preamble.h" |
| 24 | #include "SourceCode.h" |
| 25 | #include "TidyProvider.h" |
| 26 | #include "clang-include-cleaner/Record.h" |
| 27 | #include "index/CanonicalIncludes.h" |
| 28 | #include "index/Index.h" |
| 29 | #include "index/Symbol.h" |
| 30 | #include "support/Logger.h" |
| 31 | #include "support/Trace.h" |
| 32 | #include "clang/AST/ASTContext.h" |
| 33 | #include "clang/AST/Decl.h" |
| 34 | #include "clang/Basic/Diagnostic.h" |
| 35 | #include "clang/Basic/DiagnosticSema.h" |
| 36 | #include "clang/Basic/LangOptions.h" |
| 37 | #include "clang/Basic/SourceLocation.h" |
| 38 | #include "clang/Basic/SourceManager.h" |
| 39 | #include "clang/Basic/TokenKinds.h" |
| 40 | #include "clang/Frontend/CompilerInstance.h" |
| 41 | #include "clang/Frontend/CompilerInvocation.h" |
| 42 | #include "clang/Frontend/FrontendActions.h" |
| 43 | #include "clang/Lex/Lexer.h" |
| 44 | #include "clang/Lex/PPCallbacks.h" |
| 45 | #include "clang/Lex/Preprocessor.h" |
| 46 | #include "clang/Serialization/ASTWriter.h" |
| 47 | #include "clang/Tooling/CompilationDatabase.h" |
| 48 | #include "clang/Tooling/Syntax/Tokens.h" |
| 49 | #include "llvm/ADT/ArrayRef.h" |
| 50 | #include "llvm/ADT/STLExtras.h" |
| 51 | #include "llvm/ADT/SmallVector.h" |
| 52 | #include "llvm/ADT/StringRef.h" |
| 53 | #include <algorithm> |
| 54 | #include <memory> |
| 55 | #include <optional> |
| 56 | #include <vector> |
| 57 | |
| 58 | // Force the linker to link in Clang-tidy modules. |
| 59 | // clangd doesn't support the static analyzer. |
| 60 | #if CLANGD_TIDY_CHECKS |
| 61 | #define CLANG_TIDY_DISABLE_STATIC_ANALYZER_CHECKS |
| 62 | #include "../clang-tidy/ClangTidyForceLinker.h" |
| 63 | #endif |
| 64 | |
| 65 | namespace clang { |
| 66 | namespace clangd { |
| 67 | namespace { |
| 68 | |
| 69 | template <class T> std::size_t getUsedBytes(const std::vector<T> &Vec) { |
| 70 | return Vec.capacity() * sizeof(T); |
| 71 | } |
| 72 | |
| 73 | class DeclTrackingASTConsumer : public ASTConsumer { |
| 74 | public: |
| 75 | DeclTrackingASTConsumer(std::vector<Decl *> &TopLevelDecls) |
| 76 | : TopLevelDecls(TopLevelDecls) {} |
| 77 | |
| 78 | bool HandleTopLevelDecl(DeclGroupRef DG) override { |
| 79 | for (Decl *D : DG) { |
| 80 | auto &SM = D->getASTContext().getSourceManager(); |
| 81 | if (!isInsideMainFile(D->getLocation(), SM)) |
| 82 | continue; |
| 83 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 84 | if (isImplicitTemplateInstantiation(ND)) |
| 85 | continue; |
| 86 | |
| 87 | // ObjCMethodDecl are not actually top-level decls. |
| 88 | if (isa<ObjCMethodDecl>(D)) |
| 89 | continue; |
| 90 | |
| 91 | TopLevelDecls.push_back(D); |
| 92 | } |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | private: |
| 97 | std::vector<Decl *> &TopLevelDecls; |
| 98 | }; |
| 99 | |
| 100 | class ClangdFrontendAction : public SyntaxOnlyAction { |
| 101 | public: |
| 102 | std::vector<Decl *> takeTopLevelDecls() { return std::move(TopLevelDecls); } |
| 103 | |
| 104 | protected: |
| 105 | std::unique_ptr<ASTConsumer> |
| 106 | CreateASTConsumer(CompilerInstance &CI, llvm::StringRef InFile) override { |
| 107 | return std::make_unique<DeclTrackingASTConsumer>(/*ref*/ TopLevelDecls); |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | std::vector<Decl *> TopLevelDecls; |
| 112 | }; |
| 113 | |
| 114 | // When using a preamble, only preprocessor events outside its bounds are seen. |
| 115 | // This is almost what we want: replaying transitive preprocessing wastes time. |
| 116 | // However this confuses clang-tidy checks: they don't see any #includes! |
| 117 | // So we replay the *non-transitive* #includes that appear in the main-file. |
| 118 | // It would be nice to replay other events (macro definitions, ifdefs etc) but |
| 119 | // this addresses the most common cases fairly cheaply. |
| 120 | class ReplayPreamble : private PPCallbacks { |
| 121 | public: |
| 122 | // Attach preprocessor hooks such that preamble events will be injected at |
| 123 | // the appropriate time. |
| 124 | // Events will be delivered to the *currently registered* PP callbacks. |
| 125 | static void attach(std::vector<Inclusion> Includes, CompilerInstance &Clang, |
| 126 | const PreambleBounds &PB) { |
| 127 | auto &PP = Clang.getPreprocessor(); |
| 128 | auto *ExistingCallbacks = PP.getPPCallbacks(); |
| 129 | // No need to replay events if nobody is listening. |
| 130 | if (!ExistingCallbacks) |
| 131 | return; |
| 132 | PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(new ReplayPreamble( |
| 133 | std::move(Includes), ExistingCallbacks, Clang.getSourceManager(), PP, |
| 134 | Clang.getLangOpts(), PB))); |
| 135 | // We're relying on the fact that addPPCallbacks keeps the old PPCallbacks |
| 136 | // around, creating a chaining wrapper. Guard against other implementations. |
| 137 | assert(PP.getPPCallbacks() != ExistingCallbacks && |
| 138 | "Expected chaining implementation"); |
| 139 | } |
| 140 | |
| 141 | private: |
| 142 | ReplayPreamble(std::vector<Inclusion> Includes, PPCallbacks *Delegate, |
| 143 | const SourceManager &SM, Preprocessor &PP, |
| 144 | const LangOptions &LangOpts, const PreambleBounds &PB) |
| 145 | : Includes(std::move(Includes)), Delegate(Delegate), SM(SM), PP(PP) { |
| 146 | // Only tokenize the preamble section of the main file, as we are not |
| 147 | // interested in the rest of the tokens. |
| 148 | MainFileTokens = syntax::tokenize( |
| 149 | syntax::FileRange(SM.getMainFileID(), 0, PB.Size), SM, LangOpts); |
| 150 | } |
| 151 | |
| 152 | // In a normal compile, the preamble traverses the following structure: |
| 153 | // |
| 154 | // mainfile.cpp |
| 155 | // <built-in> |
| 156 | // ... macro definitions like __cplusplus ... |
| 157 | // <command-line> |
| 158 | // ... macro definitions for args like -Dfoo=bar ... |
| 159 | // "header1.h" |
| 160 | // ... header file contents ... |
| 161 | // "header2.h" |
| 162 | // ... header file contents ... |
| 163 | // ... main file contents ... |
| 164 | // |
| 165 | // When using a preamble, the "header1" and "header2" subtrees get skipped. |
| 166 | // We insert them right after the built-in header, which still appears. |
| 167 | void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 168 | SrcMgr::CharacteristicKind Kind, FileID PrevFID) override { |
| 169 | // It'd be nice if there was a better way to identify built-in headers... |
| 170 | if (Reason == FileChangeReason::ExitFile && |
| 171 | SM.getBufferOrFake(PrevFID).getBufferIdentifier() == "<built-in>") |
| 172 | replay(); |
| 173 | } |
| 174 | |
| 175 | void replay() { |
| 176 | for (const auto &Inc : Includes) { |
| 177 | OptionalFileEntryRef File; |
| 178 | if (Inc.Resolved != "") |
| 179 | File = expectedToOptional(SM.getFileManager().getFileRef(Inc.Resolved)); |
| 180 | |
| 181 | // Re-lex the #include directive to find its interesting parts. |
| 182 | auto HashLoc = SM.getComposedLoc(SM.getMainFileID(), Inc.HashOffset); |
| 183 | auto HashTok = llvm::partition_point(MainFileTokens, |
| 184 | [&HashLoc](const syntax::Token &T) { |
| 185 | return T.location() < HashLoc; |
| 186 | }); |
| 187 | assert(HashTok != MainFileTokens.end() && HashTok->kind() == tok::hash); |
| 188 | |
| 189 | auto IncludeTok = std::next(HashTok); |
| 190 | assert(IncludeTok != MainFileTokens.end()); |
| 191 | |
| 192 | auto FileTok = std::next(IncludeTok); |
| 193 | assert(FileTok != MainFileTokens.end()); |
| 194 | |
| 195 | // Create a fake import/include token, none of the callers seem to care |
| 196 | // about clang::Token::Flags. |
| 197 | Token SynthesizedIncludeTok; |
| 198 | SynthesizedIncludeTok.startToken(); |
| 199 | SynthesizedIncludeTok.setLocation(IncludeTok->location()); |
| 200 | SynthesizedIncludeTok.setLength(IncludeTok->length()); |
| 201 | SynthesizedIncludeTok.setKind(tok::raw_identifier); |
| 202 | SynthesizedIncludeTok.setRawIdentifierData(IncludeTok->text(SM).data()); |
| 203 | PP.LookUpIdentifierInfo(SynthesizedIncludeTok); |
| 204 | |
| 205 | // Same here, create a fake one for Filename, including angles or quotes. |
| 206 | Token SynthesizedFilenameTok; |
| 207 | SynthesizedFilenameTok.startToken(); |
| 208 | SynthesizedFilenameTok.setLocation(FileTok->location()); |
| 209 | // Note that we can't make use of FileTok->length/text in here as in the |
| 210 | // case of angled includes this will contain tok::less instead of |
| 211 | // filename. Whereas Inc.Written contains the full header name including |
| 212 | // quotes/angles. |
| 213 | SynthesizedFilenameTok.setLength(Inc.Written.length()); |
| 214 | SynthesizedFilenameTok.setKind(tok::header_name); |
| 215 | SynthesizedFilenameTok.setLiteralData(Inc.Written.data()); |
| 216 | |
| 217 | llvm::StringRef WrittenFilename = |
| 218 | llvm::StringRef(Inc.Written).drop_front().drop_back(); |
| 219 | Delegate->InclusionDirective( |
| 220 | HashTok->location(), SynthesizedIncludeTok, WrittenFilename, |
| 221 | Inc.Written.front() == '<', |
| 222 | syntax::FileRange(SM, SynthesizedFilenameTok.getLocation(), |
| 223 | SynthesizedFilenameTok.getEndLoc()) |
| 224 | .toCharRange(SM), |
| 225 | File, "SearchPath", "RelPath", |
| 226 | /*Imported=*/nullptr, Inc.FileKind); |
| 227 | if (File) |
| 228 | Delegate->FileSkipped(*File, SynthesizedFilenameTok, Inc.FileKind); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | const std::vector<Inclusion> Includes; |
| 233 | PPCallbacks *Delegate; |
| 234 | const SourceManager &SM; |
| 235 | Preprocessor &PP; |
| 236 | std::vector<syntax::Token> MainFileTokens; |
| 237 | }; |
| 238 | |
| 239 | // Filter for clang diagnostics groups enabled by CTOptions.Checks. |
| 240 | // |
| 241 | // These are check names like clang-diagnostics-unused. |
| 242 | // Note that unlike -Wunused, clang-diagnostics-unused does not imply |
| 243 | // subcategories like clang-diagnostics-unused-function. |
| 244 | // |
| 245 | // This is used to determine which diagnostics can be enabled by ExtraArgs in |
| 246 | // the clang-tidy configuration. |
| 247 | class TidyDiagnosticGroups { |
| 248 | // Whether all diagnostic groups are enabled by default. |
| 249 | // True if we've seen clang-diagnostic-*. |
| 250 | bool Default = false; |
| 251 | // Set of diag::Group whose enablement != Default. |
| 252 | // If Default is false, this is foo where we've seen clang-diagnostic-foo. |
| 253 | llvm::DenseSet<unsigned> Exceptions; |
| 254 | |
| 255 | public: |
| 256 | TidyDiagnosticGroups(llvm::StringRef Checks) { |
| 257 | constexpr llvm::StringLiteral CDPrefix = "clang-diagnostic-"; |
| 258 | |
| 259 | llvm::StringRef Check; |
| 260 | while (!Checks.empty()) { |
| 261 | std::tie(Check, Checks) = Checks.split(','); |
| 262 | if (Check.empty()) |
| 263 | continue; |
| 264 | |
| 265 | bool Enable = !Check.consume_front("-"); |
| 266 | bool Glob = Check.consume_back("*"); |
| 267 | if (Glob) { |
| 268 | // Is this clang-diagnostic-*, or *, or so? |
| 269 | // (We ignore all other types of globs). |
| 270 | if (CDPrefix.startswith(Check)) { |
| 271 | Default = Enable; |
| 272 | Exceptions.clear(); |
| 273 | } |
| 274 | continue; |
| 275 | } |
| 276 | |
| 277 | // In "*,clang-diagnostic-foo", the latter is a no-op. |
| 278 | if (Default == Enable) |
| 279 | continue; |
| 280 | // The only non-glob entries we care about are clang-diagnostic-foo. |
| 281 | if (!Check.consume_front(CDPrefix)) |
| 282 | continue; |
| 283 | |
| 284 | if (auto Group = DiagnosticIDs::getGroupForWarningOption(Check)) |
| 285 | Exceptions.insert(static_cast<unsigned>(*Group)); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | bool operator()(diag::Group GroupID) const { |
| 290 | return Exceptions.contains(static_cast<unsigned>(GroupID)) ? !Default |
| 291 | : Default; |
| 292 | } |
| 293 | }; |
| 294 | |
| 295 | // Find -W<group> and -Wno-<group> options in ExtraArgs and apply them to Diags. |
| 296 | // |
| 297 | // This is used to handle ExtraArgs in clang-tidy configuration. |
| 298 | // We don't use clang's standard handling of this as we want slightly different |
| 299 | // behavior (e.g. we want to exclude these from -Wno-error). |
| 300 | void applyWarningOptions(llvm::ArrayRef<std::string> ExtraArgs, |
| 301 | llvm::function_ref<bool(diag::Group)> EnabledGroups, |
| 302 | DiagnosticsEngine &Diags) { |
| 303 | for (llvm::StringRef Group : ExtraArgs) { |
| 304 | // Only handle args that are of the form -W[no-]<group>. |
| 305 | // Other flags are possible but rare and deliberately out of scope. |
| 306 | llvm::SmallVector<diag::kind> Members; |
| 307 | if (!Group.consume_front("-W") || Group.empty()) |
| 308 | continue; |
| 309 | bool Enable = !Group.consume_front("no-"); |
| 310 | if (Diags.getDiagnosticIDs()->getDiagnosticsInGroup( |
| 311 | diag::Flavor::WarningOrError, Group, Members)) |
| 312 | continue; |
| 313 | |
| 314 | // Upgrade (or downgrade) the severity of each diagnostic in the group. |
| 315 | // If -Werror is on, newly added warnings will be treated as errors. |
| 316 | // We don't want this, so keep track of them to fix afterwards. |
| 317 | bool NeedsWerrorExclusion = false; |
| 318 | for (diag::kind ID : Members) { |
| 319 | if (Enable) { |
| 320 | if (Diags.getDiagnosticLevel(ID, SourceLocation()) < |
| 321 | DiagnosticsEngine::Warning) { |
| 322 | auto Group = DiagnosticIDs::getGroupForDiag(ID); |
| 323 | if (!Group || !EnabledGroups(*Group)) |
| 324 | continue; |
| 325 | Diags.setSeverity(ID, diag::Severity::Warning, SourceLocation()); |
| 326 | if (Diags.getWarningsAsErrors()) |
| 327 | NeedsWerrorExclusion = true; |
| 328 | } |
| 329 | } else { |
| 330 | Diags.setSeverity(ID, diag::Severity::Ignored, SourceLocation()); |
| 331 | } |
| 332 | } |
| 333 | if (NeedsWerrorExclusion) { |
| 334 | // FIXME: there's no API to suppress -Werror for single diagnostics. |
| 335 | // In some cases with sub-groups, we may end up erroneously |
| 336 | // downgrading diagnostics that were -Werror in the compile command. |
| 337 | Diags.setDiagnosticGroupWarningAsError(Group, false); |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | } // namespace |
| 343 | |
| 344 | std::optional<ParsedAST> |
| 345 | ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs, |
| 346 | std::unique_ptr<clang::CompilerInvocation> CI, |
| 347 | llvm::ArrayRef<Diag> CompilerInvocationDiags, |
| 348 | std::shared_ptr<const PreambleData> Preamble) { |
| 349 | trace::Span Tracer("BuildAST"); |
| 350 | SPAN_ATTACH(Tracer, "File", Filename); |
| 351 | |
| 352 | auto VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory); |
| 353 | if (Preamble && Preamble->StatCache) |
| 354 | VFS = Preamble->StatCache->getConsumingFS(std::move(VFS)); |
| 355 | |
| 356 | assert(CI); |
| 357 | // Command-line parsing sets DisableFree to true by default, but we don't want |
| 358 | // to leak memory in clangd. |
| 359 | CI->getFrontendOpts().DisableFree = false; |
| 360 | const PrecompiledPreamble *PreamblePCH = |
| 361 | Preamble ? &Preamble->Preamble : nullptr; |
| 362 | |
| 363 | // This is on-by-default in windows to allow parsing SDK headers, but it |
| 364 | // breaks many features. Disable it for the main-file (not preamble). |
| 365 | CI->getLangOpts()->DelayedTemplateParsing = false; |
| 366 | |
| 367 | std::vector<std::unique_ptr<FeatureModule::ASTListener>> ASTListeners; |
| 368 | if (Inputs.FeatureModules) { |
| 369 | for (auto &M : *Inputs.FeatureModules) { |
| 370 | if (auto Listener = M.astListeners()) |
| 371 | ASTListeners.emplace_back(std::move(Listener)); |
| 372 | } |
| 373 | } |
| 374 | StoreDiags ASTDiags; |
| 375 | ASTDiags.setDiagCallback( |
| 376 | [&ASTListeners](const clang::Diagnostic &D, clangd::Diag &Diag) { |
| 377 | llvm::for_each(ASTListeners, |
| 378 | [&](const auto &L) { L->sawDiagnostic(D, Diag); }); |
| 379 | }); |
| 380 | |
| 381 | std::optional<PreamblePatch> Patch; |
| 382 | bool PreserveDiags = true; |
| 383 | // We might use an ignoring diagnostic consumer if they are going to be |
| 384 | // dropped later on to not pay for extra latency by processing them. |
| 385 | DiagnosticConsumer *DiagConsumer = &ASTDiags; |
| 386 | IgnoreDiagnostics DropDiags; |
| 387 | if (Preamble) { |
| 388 | Patch = PreamblePatch::createFullPatch(Filename, Inputs, *Preamble); |
| 389 | Patch->apply(*CI); |
| 390 | PreserveDiags = Patch->preserveDiagnostics(); |
| 391 | if (!PreserveDiags) |
| 392 | DiagConsumer = &DropDiags; |
| 393 | } |
| 394 | auto Clang = prepareCompilerInstance( |
| 395 | std::move(CI), PreamblePCH, |
| 396 | llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents, Filename), VFS, |
| 397 | *DiagConsumer); |
| 398 | if (!Clang) { |
| 399 | // The last diagnostic contains information about the reason of this |
| 400 | // failure. |
| 401 | std::vector<Diag> Diags(ASTDiags.take()); |
| 402 | elog("Failed to prepare a compiler instance: {0}", |
| 403 | !Diags.empty() ? static_cast<DiagBase &>(Diags.back()).Message |
| 404 | : "unknown error"); |
| 405 | return std::nullopt; |
| 406 | } |
| 407 | tidy::ClangTidyOptions ClangTidyOpts; |
| 408 | if (PreserveDiags) { |
| 409 | trace::Span Tracer("ClangTidyOpts"); |
| 410 | ClangTidyOpts = getTidyOptionsForFile(Inputs.ClangTidyProvider, Filename); |
| 411 | dlog("ClangTidy configuration for file {0}: {1}", Filename, |
| 412 | tidy::configurationAsText(ClangTidyOpts)); |
| 413 | |
| 414 | // If clang-tidy is configured to emit clang warnings, we should too. |
| 415 | // |
| 416 | // Such clang-tidy configuration consists of two parts: |
| 417 | // - ExtraArgs: ["-Wfoo"] causes clang to produce the warnings |
| 418 | // - Checks: "clang-diagnostic-foo" prevents clang-tidy filtering them out |
| 419 | // |
| 420 | // In clang-tidy, diagnostics are emitted if they pass both checks. |
| 421 | // When groups contain subgroups, -Wparent includes the child, but |
| 422 | // clang-diagnostic-parent does not. |
| 423 | // |
| 424 | // We *don't* want to change the compile command directly. This can have |
| 425 | // too many unexpected effects: breaking the command, interactions with |
| 426 | // -- and -Werror, etc. Besides, we've already parsed the command. |
| 427 | // Instead we parse the -W<group> flags and handle them directly. |
| 428 | // |
| 429 | // Similarly, we don't want to use Checks to filter clang diagnostics after |
| 430 | // they are generated, as this spreads clang-tidy emulation everywhere. |
| 431 | // Instead, we just use these to filter which extra diagnostics we enable. |
| 432 | auto &Diags = Clang->getDiagnostics(); |
| 433 | TidyDiagnosticGroups TidyGroups(ClangTidyOpts.Checks ? *ClangTidyOpts.Checks |
| 434 | : llvm::StringRef()); |
| 435 | if (ClangTidyOpts.ExtraArgsBefore) |
| 436 | applyWarningOptions(*ClangTidyOpts.ExtraArgsBefore, TidyGroups, Diags); |
| 437 | if (ClangTidyOpts.ExtraArgs) |
| 438 | applyWarningOptions(*ClangTidyOpts.ExtraArgs, TidyGroups, Diags); |
| 439 | } else { |
| 440 | // Skips some analysis. |
| 441 | Clang->getDiagnosticOpts().IgnoreWarnings = true; |
| 442 | } |
| 443 | |
| 444 | auto Action = std::make_unique<ClangdFrontendAction>(); |
| 445 | const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0]; |
| 446 | if (!Action->BeginSourceFile(*Clang, MainInput)) { |
| 447 | log("BeginSourceFile() failed when building AST for {0}", |
| 448 | MainInput.getFile()); |
| 449 | return std::nullopt; |
| 450 | } |
| 451 | // If we saw an include guard in the preamble section of the main file, |
| 452 | // mark the main-file as include-guarded. |
| 453 | // This information is part of the HeaderFileInfo but is not loaded from the |
| 454 | // preamble as the file's size is part of its identity and may have changed. |
| 455 | // (The rest of HeaderFileInfo is not relevant for our purposes). |
| 456 | if (Preamble && Preamble->MainIsIncludeGuarded) { |
| 457 | const SourceManager &SM = Clang->getSourceManager(); |
| 458 | const FileEntry *MainFE = SM.getFileEntryForID(SM.getMainFileID()); |
| 459 | Clang->getPreprocessor().getHeaderSearchInfo().MarkFileIncludeOnce(MainFE); |
| 460 | } |
| 461 | |
| 462 | // Set up ClangTidy. Must happen after BeginSourceFile() so ASTContext exists. |
| 463 | // Clang-tidy has some limitations to ensure reasonable performance: |
| 464 | // - checks don't see all preprocessor events in the preamble |
| 465 | // - matchers run only over the main-file top-level decls (and can't see |
| 466 | // ancestors outside this scope). |
| 467 | // In practice almost all checks work well without modifications. |
| 468 | std::vector<std::unique_ptr<tidy::ClangTidyCheck>> CTChecks; |
| 469 | ast_matchers::MatchFinder CTFinder; |
| 470 | std::optional<tidy::ClangTidyContext> CTContext; |
| 471 | // Must outlive FixIncludes. |
| 472 | auto BuildDir = VFS->getCurrentWorkingDirectory(); |
| 473 | std::optional<IncludeFixer> FixIncludes; |
| 474 | llvm::DenseMap<diag::kind, DiagnosticsEngine::Level> OverriddenSeverity; |
| 475 | // No need to run clang-tidy or IncludeFixerif we are not going to surface |
| 476 | // diagnostics. |
| 477 | if (PreserveDiags) { |
| 478 | trace::Span Tracer("ClangTidyInit"); |
| 479 | tidy::ClangTidyCheckFactories CTFactories; |
| 480 | for (const auto &E : tidy::ClangTidyModuleRegistry::entries()) |
| 481 | E.instantiate()->addCheckFactories(CTFactories); |
| 482 | CTContext.emplace(std::make_unique<tidy::DefaultOptionsProvider>( |
| 483 | tidy::ClangTidyGlobalOptions(), ClangTidyOpts)); |
| 484 | CTContext->setDiagnosticsEngine(&Clang->getDiagnostics()); |
| 485 | CTContext->setASTContext(&Clang->getASTContext()); |
| 486 | CTContext->setCurrentFile(Filename); |
| 487 | CTContext->setSelfContainedDiags(true); |
| 488 | CTChecks = CTFactories.createChecksForLanguage(&*CTContext); |
| 489 | Preprocessor *PP = &Clang->getPreprocessor(); |
| 490 | for (const auto &Check : CTChecks) { |
| 491 | Check->registerPPCallbacks(Clang->getSourceManager(), PP, PP); |
| 492 | Check->registerMatchers(&CTFinder); |
| 493 | } |
| 494 | |
| 495 | // Clang only corrects typos for use of undeclared functions in C if that |
| 496 | // use is an error. Include fixer relies on typo correction, so pretend |
| 497 | // this is an error. (The actual typo correction is nice too). |
| 498 | // We restore the original severity in the level adjuster. |
| 499 | // FIXME: It would be better to have a real API for this, but what? |
| 500 | for (auto ID : {diag::ext_implicit_function_decl_c99, |
| 501 | diag::ext_implicit_lib_function_decl, |
| 502 | diag::ext_implicit_lib_function_decl_c99, |
| 503 | diag::warn_implicit_function_decl}) { |
| 504 | OverriddenSeverity.try_emplace( |
| 505 | ID, Clang->getDiagnostics().getDiagnosticLevel(ID, SourceLocation())); |
| 506 | Clang->getDiagnostics().setSeverity(ID, diag::Severity::Error, |
| 507 | SourceLocation()); |
| 508 | } |
| 509 | |
| 510 | const Config &Cfg = Config::current(); |
| 511 | ASTDiags.setLevelAdjuster([&](DiagnosticsEngine::Level DiagLevel, |
| 512 | const clang::Diagnostic &Info) { |
| 513 | if (Cfg.Diagnostics.SuppressAll || |
| 514 | isBuiltinDiagnosticSuppressed(Info.getID(), Cfg.Diagnostics.Suppress, |
| 515 | Clang->getLangOpts())) |
| 516 | return DiagnosticsEngine::Ignored; |
| 517 | |
| 518 | auto It = OverriddenSeverity.find(Info.getID()); |
| 519 | if (It != OverriddenSeverity.end()) |
| 520 | DiagLevel = It->second; |
| 521 | |
| 522 | if (!CTChecks.empty()) { |
| 523 | std::string CheckName = CTContext->getCheckName(Info.getID()); |
| 524 | bool IsClangTidyDiag = !CheckName.empty(); |
| 525 | if (IsClangTidyDiag) { |
| 526 | if (Cfg.Diagnostics.Suppress.contains(CheckName)) |
| 527 | return DiagnosticsEngine::Ignored; |
| 528 | // Check for suppression comment. Skip the check for diagnostics not |
| 529 | // in the main file, because we don't want that function to query the |
| 530 | // source buffer for preamble files. For the same reason, we ask |
| 531 | // shouldSuppressDiagnostic to avoid I/O. |
| 532 | // We let suppression comments take precedence over warning-as-error |
| 533 | // to match clang-tidy's behaviour. |
| 534 | bool IsInsideMainFile = |
| 535 | Info.hasSourceManager() && |
| 536 | isInsideMainFile(Info.getLocation(), Info.getSourceManager()); |
| 537 | SmallVector<tooling::Diagnostic, 1> TidySuppressedErrors; |
| 538 | if (IsInsideMainFile && CTContext->shouldSuppressDiagnostic( |
| 539 | DiagLevel, Info, TidySuppressedErrors, |
| 540 | /*AllowIO=*/false, |
| 541 | /*EnableNolintBlocks=*/true)) { |
| 542 | // FIXME: should we expose the suppression error (invalid use of |
| 543 | // NOLINT comments)? |
| 544 | return DiagnosticsEngine::Ignored; |
| 545 | } |
| 546 | if (!CTContext->getOptions().SystemHeaders.value_or(false) && |
| 547 | Info.hasSourceManager() && |
| 548 | Info.getSourceManager().isInSystemMacro(Info.getLocation())) |
| 549 | return DiagnosticsEngine::Ignored; |
| 550 | |
| 551 | // Check for warning-as-error. |
| 552 | if (DiagLevel == DiagnosticsEngine::Warning && |
| 553 | CTContext->treatAsError(CheckName)) { |
| 554 | return DiagnosticsEngine::Error; |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | return DiagLevel; |
| 559 | }); |
| 560 | |
| 561 | // Add IncludeFixer which can recover diagnostics caused by missing includes |
| 562 | // (e.g. incomplete type) and attach include insertion fixes to diagnostics. |
| 563 | if (Inputs.Index && !BuildDir.getError()) { |
| 564 | auto Style = |
| 565 | getFormatStyleForFile(Filename, Inputs.Contents, *Inputs.TFS); |
| 566 | auto Inserter = std::make_shared<IncludeInserter>( |
| 567 | Filename, Inputs.Contents, Style, BuildDir.get(), |
| 568 | &Clang->getPreprocessor().getHeaderSearchInfo()); |
| 569 | ArrayRef<Inclusion> MainFileIncludes; |
| 570 | if (Preamble) { |
| 571 | MainFileIncludes = Preamble->Includes.MainFileIncludes; |
| 572 | for (const auto &Inc : Preamble->Includes.MainFileIncludes) |
| 573 | Inserter->addExisting(Inc); |
| 574 | } |
| 575 | // FIXME: Consider piping through ASTSignals to fetch this to handle the |
| 576 | // case where a header file contains ObjC decls but no #imports. |
| 577 | Symbol::IncludeDirective Directive = |
| 578 | Inputs.Opts.ImportInsertions |
| 579 | ? preferredIncludeDirective(Filename, Clang->getLangOpts(), |
| 580 | MainFileIncludes, {}) |
| 581 | : Symbol::Include; |
| 582 | FixIncludes.emplace(Filename, Inserter, *Inputs.Index, |
| 583 | /*IndexRequestLimit=*/5, Directive); |
| 584 | ASTDiags.contributeFixes([&FixIncludes](DiagnosticsEngine::Level DiagLevl, |
| 585 | const clang::Diagnostic &Info) { |
| 586 | return FixIncludes->fix(DiagLevl, Info); |
| 587 | }); |
| 588 | Clang->setExternalSemaSource(FixIncludes->unresolvedNameRecorder()); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | IncludeStructure Includes; |
| 593 | // If we are using a preamble, copy existing includes. |
| 594 | if (Preamble) { |
| 595 | Includes = Preamble->Includes; |
| 596 | Includes.MainFileIncludes = Patch->preambleIncludes(); |
| 597 | // Replay the preamble includes so that clang-tidy checks can see them. |
| 598 | ReplayPreamble::attach(Patch->preambleIncludes(), *Clang, |
| 599 | Patch->modifiedBounds()); |
| 600 | } |
| 601 | // Important: collectIncludeStructure is registered *after* ReplayPreamble! |
| 602 | // Otherwise we would collect the replayed includes again... |
| 603 | // (We can't *just* use the replayed includes, they don't have Resolved path). |
| 604 | Includes.collect(*Clang); |
| 605 | // Copy over the macros in the preamble region of the main file, and combine |
| 606 | // with non-preamble macros below. |
| 607 | MainFileMacros Macros; |
| 608 | if (Preamble) |
| 609 | Macros = Preamble->Macros; |
| 610 | Clang->getPreprocessor().addPPCallbacks( |
| 611 | std::make_unique<CollectMainFileMacros>(Clang->getSourceManager(), |
| 612 | Macros)); |
| 613 | |
| 614 | std::vector<PragmaMark> Marks; |
| 615 | // FIXME: We need to patch the marks for stale preambles. |
| 616 | if (Preamble) |
| 617 | Marks = Preamble->Marks; |
| 618 | Clang->getPreprocessor().addPPCallbacks( |
| 619 | collectPragmaMarksCallback(Clang->getSourceManager(), Marks)); |
| 620 | |
| 621 | // Copy over the includes from the preamble, then combine with the |
| 622 | // non-preamble includes below. |
| 623 | CanonicalIncludes CanonIncludes; |
| 624 | if (Preamble) |
| 625 | CanonIncludes = Preamble->CanonIncludes; |
| 626 | else |
| 627 | CanonIncludes.addSystemHeadersMapping(Clang->getLangOpts()); |
| 628 | std::unique_ptr<CommentHandler> IWYUHandler = |
| 629 | collectIWYUHeaderMaps(&CanonIncludes); |
| 630 | Clang->getPreprocessor().addCommentHandler(IWYUHandler.get()); |
| 631 | |
| 632 | // Collect tokens of the main file. |
| 633 | syntax::TokenCollector CollectTokens(Clang->getPreprocessor()); |
| 634 | |
| 635 | // To remain consistent with preamble builds, these callbacks must be called |
| 636 | // exactly here, after preprocessor is initialized and BeginSourceFile() was |
| 637 | // called already. |
| 638 | for (const auto &L : ASTListeners) |
| 639 | L->beforeExecute(*Clang); |
| 640 | |
| 641 | if (llvm::Error Err = Action->Execute()) |
| 642 | log("Execute() failed when building AST for {0}: {1}", MainInput.getFile(), |
| 643 | toString(std::move(Err))); |
| 644 | |
| 645 | // We have to consume the tokens before running clang-tidy to avoid collecting |
| 646 | // tokens from running the preprocessor inside the checks (only |
| 647 | // modernize-use-trailing-return-type does that today). |
| 648 | syntax::TokenBuffer Tokens = std::move(CollectTokens).consume(); |
| 649 | // Makes SelectionTree build much faster. |
| 650 | Tokens.indexExpandedTokens(); |
| 651 | std::vector<Decl *> ParsedDecls = Action->takeTopLevelDecls(); |
| 652 | // AST traversals should exclude the preamble, to avoid performance cliffs. |
| 653 | Clang->getASTContext().setTraversalScope(ParsedDecls); |
| 654 | if (!CTChecks.empty()) { |
| 655 | // Run the AST-dependent part of the clang-tidy checks. |
| 656 | // (The preprocessor part ran already, via PPCallbacks). |
| 657 | trace::Span Tracer("ClangTidyMatch"); |
| 658 | CTFinder.matchAST(Clang->getASTContext()); |
| 659 | } |
| 660 | |
| 661 | // XXX: This is messy: clang-tidy checks flush some diagnostics at EOF. |
| 662 | // However Action->EndSourceFile() would destroy the ASTContext! |
| 663 | // So just inform the preprocessor of EOF, while keeping everything alive. |
| 664 | Clang->getPreprocessor().EndSourceFile(); |
| 665 | // UnitDiagsConsumer is local, we can not store it in CompilerInstance that |
| 666 | // has a longer lifetime. |
| 667 | Clang->getDiagnostics().setClient(new IgnoreDiagnostics); |
| 668 | // CompilerInstance won't run this callback, do it directly. |
| 669 | ASTDiags.EndSourceFile(); |
| 670 | |
| 671 | std::optional<std::vector<Diag>> Diags; |
| 672 | // FIXME: Also skip generation of diagnostics alltogether to speed up ast |
| 673 | // builds when we are patching a stale preamble. |
| 674 | if (PreserveDiags) { |
| 675 | Diags = CompilerInvocationDiags; |
| 676 | // Add diagnostics from the preamble, if any. |
| 677 | if (Preamble) |
| 678 | Diags->insert(Diags->end(), Preamble->Diags.begin(), |
| 679 | Preamble->Diags.end()); |
| 680 | // Finally, add diagnostics coming from the AST. |
| 681 | { |
| 682 | std::vector<Diag> D = ASTDiags.take(&*CTContext); |
| 683 | Diags->insert(Diags->end(), D.begin(), D.end()); |
| 684 | } |
| 685 | } |
| 686 | ParsedAST Result(Filename, Inputs.Version, std::move(Preamble), |
| 687 | std::move(Clang), std::move(Action), std::move(Tokens), |
| 688 | std::move(Macros), std::move(Marks), std::move(ParsedDecls), |
| 689 | std::move(Diags), std::move(Includes), |
| 690 | std::move(CanonIncludes)); |
| 691 | if (Result.Diags) { |
| 692 | auto UnusedHeadersDiags = |
| 693 | issueUnusedIncludesDiagnostics(Result, Inputs.Contents); |
| 694 | Result.Diags->insert(Result.Diags->end(), |
| 695 | make_move_iterator(UnusedHeadersDiags.begin()), |
| 696 | make_move_iterator(UnusedHeadersDiags.end())); |
| 697 | } |
| 698 | return std::move(Result); |
| 699 | } |
| 700 | |
| 701 | ParsedAST::ParsedAST(ParsedAST &&Other) = default; |
| 702 | |
| 703 | ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default; |
| 704 | |
| 705 | ParsedAST::~ParsedAST() { |
| 706 | if (Action) { |
| 707 | // We already notified the PP of end-of-file earlier, so detach it first. |
| 708 | // We must keep it alive until after EndSourceFile(), Sema relies on this. |
| 709 | auto PP = Clang->getPreprocessorPtr(); // Keep PP alive for now. |
| 710 | Clang->setPreprocessor(nullptr); // Detach so we don't send EOF again. |
| 711 | Action->EndSourceFile(); // Destroy ASTContext and Sema. |
| 712 | // Now Sema is gone, it's safe for PP to go out of scope. |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); } |
| 717 | |
| 718 | const ASTContext &ParsedAST::getASTContext() const { |
| 719 | return Clang->getASTContext(); |
| 720 | } |
| 721 | |
| 722 | Sema &ParsedAST::getSema() { return Clang->getSema(); } |
| 723 | |
| 724 | Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); } |
| 725 | |
| 726 | std::shared_ptr<Preprocessor> ParsedAST::getPreprocessorPtr() { |
| 727 | return Clang->getPreprocessorPtr(); |
| 728 | } |
| 729 | |
| 730 | const Preprocessor &ParsedAST::getPreprocessor() const { |
| 731 | return Clang->getPreprocessor(); |
| 732 | } |
| 733 | |
| 734 | llvm::ArrayRef<Decl *> ParsedAST::getLocalTopLevelDecls() { |
| 735 | return LocalTopLevelDecls; |
| 736 | } |
| 737 | |
| 738 | llvm::ArrayRef<const Decl *> ParsedAST::getLocalTopLevelDecls() const { |
| 739 | return LocalTopLevelDecls; |
| 740 | } |
| 741 | |
| 742 | const MainFileMacros &ParsedAST::getMacros() const { return Macros; } |
| 743 | const std::vector<PragmaMark> &ParsedAST::getMarks() const { return Marks; } |
| 744 | |
| 745 | std::size_t ParsedAST::getUsedBytes() const { |
| 746 | auto &AST = getASTContext(); |
| 747 | // FIXME(ibiryukov): we do not account for the dynamically allocated part of |
| 748 | // Message and Fixes inside each diagnostic. |
| 749 | std::size_t Total = clangd::getUsedBytes(LocalTopLevelDecls) + |
| 750 | (Diags ? clangd::getUsedBytes(*Diags) : 0); |
| 751 | |
| 752 | // FIXME: the rest of the function is almost a direct copy-paste from |
| 753 | // libclang's clang_getCXTUResourceUsage. We could share the implementation. |
| 754 | |
| 755 | // Sum up various allocators inside the ast context and the preprocessor. |
| 756 | Total += AST.getASTAllocatedMemory(); |
| 757 | Total += AST.getSideTableAllocatedMemory(); |
| 758 | Total += AST.Idents.getAllocator().getTotalMemory(); |
| 759 | Total += AST.Selectors.getTotalMemory(); |
| 760 | |
| 761 | Total += AST.getSourceManager().getContentCacheSize(); |
| 762 | Total += AST.getSourceManager().getDataStructureSizes(); |
| 763 | Total += AST.getSourceManager().getMemoryBufferSizes().malloc_bytes; |
| 764 | |
| 765 | if (ExternalASTSource *Ext = AST.getExternalSource()) |
| 766 | Total += Ext->getMemoryBufferSizes().malloc_bytes; |
| 767 | |
| 768 | const Preprocessor &PP = getPreprocessor(); |
| 769 | Total += PP.getTotalMemory(); |
| 770 | if (PreprocessingRecord *PRec = PP.getPreprocessingRecord()) |
| 771 | Total += PRec->getTotalMemory(); |
| 772 | Total += PP.getHeaderSearchInfo().getTotalMemory(); |
| 773 | |
| 774 | return Total; |
| 775 | } |
| 776 | |
| 777 | const IncludeStructure &ParsedAST::getIncludeStructure() const { |
| 778 | return Includes; |
| 779 | } |
| 780 | |
| 781 | const CanonicalIncludes &ParsedAST::getCanonicalIncludes() const { |
| 782 | return CanonIncludes; |
| 783 | } |
| 784 | |
| 785 | ParsedAST::ParsedAST(PathRef TUPath, llvm::StringRef Version, |
| 786 | std::shared_ptr<const PreambleData> Preamble, |
| 787 | std::unique_ptr<CompilerInstance> Clang, |
| 788 | std::unique_ptr<FrontendAction> Action, |
| 789 | syntax::TokenBuffer Tokens, MainFileMacros Macros, |
| 790 | std::vector<PragmaMark> Marks, |
| 791 | std::vector<Decl *> LocalTopLevelDecls, |
| 792 | std::optional<std::vector<Diag>> Diags, |
| 793 | IncludeStructure Includes, CanonicalIncludes CanonIncludes) |
| 794 | : TUPath(TUPath), Version(Version), Preamble(std::move(Preamble)), |
| 795 | Clang(std::move(Clang)), Action(std::move(Action)), |
| 796 | Tokens(std::move(Tokens)), Macros(std::move(Macros)), |
| 797 | Marks(std::move(Marks)), Diags(std::move(Diags)), |
| 798 | LocalTopLevelDecls(std::move(LocalTopLevelDecls)), |
| 799 | Includes(std::move(Includes)), CanonIncludes(std::move(CanonIncludes)) { |
| 800 | Resolver = std::make_unique<HeuristicResolver>(getASTContext()); |
| 801 | assert(this->Clang); |
| 802 | assert(this->Action); |
| 803 | } |
| 804 | |
| 805 | const include_cleaner::PragmaIncludes *ParsedAST::getPragmaIncludes() const { |
| 806 | if (!Preamble) |
| 807 | return nullptr; |
| 808 | return &Preamble->Pragmas; |
| 809 | } |
| 810 | |
| 811 | std::optional<llvm::StringRef> ParsedAST::preambleVersion() const { |
| 812 | if (!Preamble) |
| 813 | return std::nullopt; |
| 814 | return llvm::StringRef(Preamble->Version); |
| 815 | } |
| 816 | |
| 817 | } // namespace clangd |
| 818 | } // namespace clang |