clang 8.0.0

ModelInjector.cpp
Go to the documentation of this file.
1 //===-- ModelInjector.cpp ---------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "ModelInjector.h"
11 #include "clang/AST/Decl.h"
12 #include "clang/Basic/IdentifierTable.h"
13 #include "clang/Basic/Stack.h"
14 #include "clang/Frontend/ASTUnit.h"
15 #include "clang/Frontend/CompilerInstance.h"
16 #include "clang/Frontend/FrontendAction.h"
17 #include "clang/Lex/Preprocessor.h"
18 #include "clang/Serialization/ASTReader.h"
19 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/Support/CrashRecoveryContext.h"
22 #include "llvm/Support/FileSystem.h"
23 #include <utility>
24
25 using namespace clang;
26 using namespace ento;
27
28 ModelInjector::ModelInjector(CompilerInstance &CI) : CI(CI) {}
29
30 Stmt *ModelInjector::getBody(const FunctionDecl *D) {
31 onBodySynthesis(D);
32 return Bodies[D->getName()];
33 }
34
35 Stmt *ModelInjector::getBody(const ObjCMethodDecl *D) {
36 onBodySynthesis(D);
37 return Bodies[D->getName()];
38 }
39
40 void ModelInjector::onBodySynthesis(const NamedDecl *D) {
41
42 // FIXME: what about overloads? Declarations can be used as keys but what
43 // about file name index? Mangled names may not be suitable for that either.
44 if (Bodies.count(D->getName()) != 0)
45 return;
46
47 SourceManager &SM = CI.getSourceManager();
48 FileID mainFileID = SM.getMainFileID();
49
50 AnalyzerOptionsRef analyzerOpts = CI.getAnalyzerOpts();
51 llvm::StringRef modelPath = analyzerOpts->ModelPath;
52
53 llvm::SmallString<128> fileName;
54
55 if (!modelPath.empty())
56 fileName =
57 llvm::StringRef(modelPath.str() + "/" + D->getName().str() + ".model");
58 else
59 fileName = llvm::StringRef(D->getName().str() + ".model");
60
61 if (!llvm::sys::fs::exists(fileName.str())) {
62 Bodies[D->getName()] = nullptr;
63 return;
64 }
65
66 auto Invocation = std::make_shared<CompilerInvocation>(CI.getInvocation());
67
68 FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
69 InputKind IK = InputKind::CXX; // FIXME
70 FrontendOpts.Inputs.clear();
71 FrontendOpts.Inputs.emplace_back(fileName, IK);
72 FrontendOpts.DisableFree = true;
73
74 Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
75
76 // Modules are parsed by a separate CompilerInstance, so this code mimics that
77 // behavior for models
78 CompilerInstance Instance(CI.getPCHContainerOperations());
79 Instance.setInvocation(std::move(Invocation));
80 Instance.createDiagnostics(
81 new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
82 /*ShouldOwnClient=*/true);
83
84 Instance.getDiagnostics().setSourceManager(&SM);
85
86 Instance.setVirtualFileSystem(&CI.getVirtualFileSystem());
87
88 // The instance wants to take ownership, however DisableFree frontend option
89 // is set to true to avoid double free issues
90 Instance.setFileManager(&CI.getFileManager());
91 Instance.setSourceManager(&SM);
92 Instance.setPreprocessor(CI.getPreprocessorPtr());
93 Instance.setASTContext(&CI.getASTContext());
94
95 Instance.getPreprocessor().InitializeForModelFile();
96
97 ParseModelFileAction parseModelFile(Bodies);
98
99 llvm::CrashRecoveryContext CRC;
100
101 CRC.RunSafelyOnThread([&]() { Instance.ExecuteAction(parseModelFile); },
102 DesiredStackSize);
103
104 Instance.getPreprocessor().FinalizeForModelFile();
105
106 Instance.resetAndLeakSourceManager();
107 Instance.resetAndLeakFileManager();
108 Instance.resetAndLeakPreprocessor();
109
110 // The preprocessor enters to the main file id when parsing is started, so
111 // the main file id is changed to the model file during parsing and it needs
112 // to be reset to the former main file id after parsing of the model file
113 // is done.
114 SM.setMainFileID(mainFileID);
115 }
ento
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1738
clang::CompilerInstance::getInvocation
CompilerInvocation & getInvocation()
Definition: CompilerInstance.h:240
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:66
clang::CompilerInstance::getPCHContainerOperations
std::shared_ptr< PCHContainerOperations > getPCHContainerOperations() const
Definition: CompilerInstance.h:529
ASTReader.h
clang::CompilerInstance::getASTContext
ASTContext & getASTContext() const
Definition: CompilerInstance.h:469
clang::ObjCMethodDecl
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:139
clang::SourceManager::setMainFileID
void setMainFileID(FileID FID)
Set the file ID for the main source file.
Definition: SourceManager.h:796
clang::InputKind::CXX
Definition: FrontendOptions.h:158
clang::CompilerInstance::getPreprocessorPtr
std::shared_ptr< Preprocessor > getPreprocessorPtr()
Definition: CompilerInstance.h:454
llvm::SmallString< 128 >
clang::CompilerInstance::getAnalyzerOpts
AnalyzerOptionsRef getAnalyzerOpts()
Definition: CompilerInstance.h:261
clang::CompilerInstance::getVirtualFileSystem
llvm::vfs::FileSystem & getVirtualFileSystem() const
Definition: CompilerInstance.h:388
clang::ento::ParseModelFileAction
Frontend action to parse model files.
Definition: FrontendActions.h:42
CompilerInstance.h
Preprocessor.h
Defines the clang::Preprocessor interface.
ModelInjector.h
This file defines the clang::ento::ModelInjector class which implements the clang::CodeInjector inter...
IdentifierTable.h
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
SM
const SourceManager & SM
Definition: Format.cpp:1490
FrontendActions.h
llvm::IntrusiveRefCntPtr
Definition: LLVM.h:45
clang::CompilerInstance
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Definition: CompilerInstance.h:73
clang::InputKind
The kind of a file that we&#39;ve been handed as an input.
Definition: FrontendOptions.h:137
clang::ForwardingDiagnosticConsumer
Diagnostic consumer that forwards diagnostics along to an existing, already-initialized diagnostic co...
Definition: Diagnostic.h:1555
clang::DesiredStackSize
constexpr size_t DesiredStackSize
The amount of stack space that Clang would like to be provided with.
Definition: Stack.h:24
clang::FileID
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Definition: SourceLocation.h:40
Decl.h
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::CompilerInstance::getFileManager
FileManager & getFileManager() const
Return the current file manager to the caller.
Definition: CompilerInstance.h:409
clang::SourceManager::getMainFileID
FileID getMainFileID() const
Returns the FileID of the main source file.
Definition: SourceManager.h:793
clang::CompilerInstance::getSourceManager
SourceManager & getSourceManager() const
Return the current source manager.
Definition: CompilerInstance.h:429
clang::FrontendOptions
FrontendOptions - Options for controlling the behavior of the frontend.
Definition: FrontendOptions.h:242
ASTUnit.h
clang::CompilerInstance::setInvocation
void setInvocation(std::shared_ptr< CompilerInvocation > Value)
setInvocation - Replace the current invocation.
Definition: CompilerInstance.cpp:75
clang::CompilerInstance::getDiagnosticClient
DiagnosticConsumer & getDiagnosticClient() const
Definition: CompilerInstance.h:353
clang::NamedDecl::getName
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:276
FrontendAction.h
Defines the clang::FrontendAction interface and various convenience abstract classes (clang::ASTFront...
Stack.h
Defines utilities for dealing with stack allocation and stack space.
clang::ento::ModelInjector::getBody
Stmt * getBody(const FunctionDecl *D) override
clang::NamedDecl
This represents a decl that may have a name.
Definition: Decl.h:249
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:582
clang::ento::ModelInjector::ModelInjector
ModelInjector(CompilerInstance &CI)
Definition: ModelInjector.cpp:28