clang 24.0.0git
AnalysisManager.cpp
Go to the documentation of this file.
1//===-- AnalysisManager.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
12#include "llvm/ADT/STLExtras.h"
13#include "llvm/ADT/SmallVector.h"
14#include "llvm/ADT/StringSwitch.h"
15
16using namespace clang;
17using namespace ento;
18
19void AnalysisManager::anchor() { }
20
23 StoreManagerCreator storemgr,
24 ConstraintManagerCreator constraintmgr,
25 CheckerManager *checkerMgr,
26 AnalyzerOptions &Options,
27 std::unique_ptr<CodeInjector> injector)
28 : AnaCtxMgr(
29 ASTCtx, Options.UnoptimizedCFG,
30 Options.ShouldIncludeImplicitDtorsInCFG,
31 /*addInitializers=*/true, Options.ShouldIncludeTemporaryDtorsInCFG,
32 Options.ShouldIncludeLifetimeInCFG,
33 // Adding LoopExit elements to the CFG is a requirement for loop
34 // unrolling.
35 Options.ShouldIncludeLoopExitInCFG || Options.ShouldUnrollLoops,
36 Options.ShouldIncludeScopesInCFG, Options.ShouldSynthesizeBodies,
37 Options.ShouldConditionalizeStaticInitializers,
38 /*addCXXNewAllocator=*/true,
39 Options.ShouldIncludeRichConstructorsInCFG,
40 Options.ShouldElideConstructors,
41 /*addVirtualBaseBranches=*/true, std::move(injector)),
42 Ctx(ASTCtx), PP(PP), LangOpts(ASTCtx.getLangOpts()),
43 PathConsumers(std::move(PDC)), CreateStoreMgr(storemgr),
44 CreateConstraintMgr(constraintmgr), CheckerMgr(checkerMgr),
45 options(Options) {
46 SmallVector<StringRef> Extensions;
47 if (!Options.UnityBuildFileExtensions.empty())
48 Options.UnityBuildFileExtensions.split(Extensions, ';');
49 for (StringRef Extension : Extensions) {
50 Extension = Extension.trim();
51 if (Extension.empty()) {
52 ASTCtx.getDiagnostics().Report(diag::err_analyzer_config_invalid_input)
53 << "unitybuild-file-extensions" << "a non-empty file extension";
54 continue;
55 }
56 UnityBuildFileExtensions.push_back(Extension);
57 }
58
59 AnaCtxMgr.getCFGBuildOptions().setAllAlwaysAdd();
60 AnaCtxMgr.getCFGBuildOptions().OmitImplicitValueInitializers = true;
61 AnaCtxMgr.getCFGBuildOptions().AddCXXDefaultInitExprInAggregates =
62 Options.ShouldIncludeDefaultInitForAggregates;
63}
64
68
71 for (const auto &Consumer : PathConsumers) {
72 Consumer->FlushDiagnostics(&filesMade);
73 }
74}
75
77 const SourceManager &SM = Ctx.getSourceManager();
78 if (SM.isInMainFile(SL))
79 return true;
80
81 // Treat configured included source files #include "foo.cpp" same as the
82 // main file.
83 StringRef FileName = SM.getFilename(SL);
84 auto EndsWithExtension = [FileName](StringRef Extension) {
85 return FileName.ends_with(Extension);
86 };
87 if (llvm::any_of(UnityBuildFileExtensions, EndsWithExtension))
88 return true;
89
90 // Support the "unified sources" compilation method (eg. WebKit) that
91 // involves producing non-header files that include other non-header files.
92 // We should be included directly from a UnifiedSource* file
93 // and we shouldn't be a header - which is a very safe defensive check.
95 if (!IL.isValid() || !SM.isInMainFile(IL))
96 return false;
97 // Should rather be "file name starts with", but the current .getFilename
98 // includes the full path.
99 if (SM.getFilename(IL).contains("UnifiedSource")) {
100 // It might be great to reuse FrontendOptions::getInputKindForExtension()
101 // but for now it doesn't discriminate between code and header files.
102 return llvm::StringSwitch<bool>(SM.getFilename(SL).rsplit('.').second)
103 .Cases({"c", "m", "mm", "C", "cc", "cp"}, true)
104 .Cases({"cpp", "CPP", "c++", "cxx", "cppm"}, true)
105 .Default(false);
106 }
107
108 return false;
109}
Defines the SourceManager interface.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
DiagnosticsEngine & getDiagnostics() const
Stores options for the analyzer from the command line.
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
bool isInMainFile(SourceLocation Loc) const
Returns whether the PresumedLoc for a given SourceLocation is in the main file.
SourceLocation getIncludeLoc(FileID FID) const
Returns the include location if FID is a #include'd file otherwise it returns an invalid location.
StringRef getFilename(SourceLocation SpellingLoc) const
Return the filename of the file containing a SourceLocation.
const LangOptions & getLangOpts() const
bool isInCodeFile(SourceLocation SL) const
AnalysisManager(ASTContext &ctx, Preprocessor &PP, PathDiagnosticConsumers Consumers, StoreManagerCreator storemgr, ConstraintManagerCreator constraintmgr, CheckerManager *checkerMgr, AnalyzerOptions &Options, std::unique_ptr< CodeInjector > injector=nullptr)
std::unique_ptr< ConstraintManager >(* ConstraintManagerCreator)(ProgramStateManager &, ExprEngine *)
std::unique_ptr< StoreManager >(* StoreManagerCreator)(ProgramStateManager &)
std::vector< std::unique_ptr< PathDiagnosticConsumer > > PathDiagnosticConsumers
Top level wrappers for InstallAPI frontend operations.
@ Default
Set to the current date and time.
#define true
Definition stdbool.h:25