clang  14.0.0git
ModelInjector.cpp
Go to the documentation of this file.
1 //===-- ModelInjector.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 "ModelInjector.h"
10 #include "clang/AST/Decl.h"
13 #include "clang/Basic/Stack.h"
14 #include "clang/AST/DeclObjC.h"
15 #include "clang/Frontend/ASTUnit.h"
18 #include "clang/Lex/Preprocessor.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/Support/CrashRecoveryContext.h"
23 #include "llvm/Support/FileSystem.h"
24 #include <utility>
25 
26 using namespace clang;
27 using namespace ento;
28 
30 
32  onBodySynthesis(D);
33  return Bodies[D->getName()];
34 }
35 
37  onBodySynthesis(D);
38  return Bodies[D->getName()];
39 }
40 
41 void ModelInjector::onBodySynthesis(const NamedDecl *D) {
42 
43  // FIXME: what about overloads? Declarations can be used as keys but what
44  // about file name index? Mangled names may not be suitable for that either.
45  if (Bodies.count(D->getName()) != 0)
46  return;
47 
49  FileID mainFileID = SM.getMainFileID();
50 
51  AnalyzerOptionsRef analyzerOpts = CI.getAnalyzerOpts();
52  llvm::StringRef modelPath = analyzerOpts->ModelPath;
53 
54  llvm::SmallString<128> fileName;
55 
56  if (!modelPath.empty())
57  fileName =
58  llvm::StringRef(modelPath.str() + "/" + D->getName().str() + ".model");
59  else
60  fileName = llvm::StringRef(D->getName().str() + ".model");
61 
62  if (!llvm::sys::fs::exists(fileName.str())) {
63  Bodies[D->getName()] = nullptr;
64  return;
65  }
66 
67  auto Invocation = std::make_shared<CompilerInvocation>(CI.getInvocation());
68 
69  FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
70  InputKind IK = Language::CXX; // FIXME
71  FrontendOpts.Inputs.clear();
72  FrontendOpts.Inputs.emplace_back(fileName, IK);
73  FrontendOpts.DisableFree = true;
74 
75  Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
76 
77  // Modules are parsed by a separate CompilerInstance, so this code mimics that
78  // behavior for models
80  Instance.setInvocation(std::move(Invocation));
81  Instance.createDiagnostics(
83  /*ShouldOwnClient=*/true);
84 
85  Instance.getDiagnostics().setSourceManager(&SM);
86 
87  // The instance wants to take ownership, however DisableFree frontend option
88  // is set to true to avoid double free issues
89  Instance.setFileManager(&CI.getFileManager());
90  Instance.setSourceManager(&SM);
91  Instance.setPreprocessor(CI.getPreprocessorPtr());
92  Instance.setASTContext(&CI.getASTContext());
93 
94  Instance.getPreprocessor().InitializeForModelFile();
95 
96  ParseModelFileAction parseModelFile(Bodies);
97 
98  llvm::CrashRecoveryContext CRC;
99 
100  CRC.RunSafelyOnThread([&]() { Instance.ExecuteAction(parseModelFile); },
102 
103  Instance.getPreprocessor().FinalizeForModelFile();
104 
105  Instance.resetAndLeakSourceManager();
106  Instance.resetAndLeakFileManager();
107  Instance.resetAndLeakPreprocessor();
108 
109  // The preprocessor enters to the main file id when parsing is started, so
110  // the main file id is changed to the model file during parsing and it needs
111  // to be reset to the former main file id after parsing of the model file
112  // is done.
113  SM.setMainFileID(mainFileID);
114 }
clang::ForwardingDiagnosticConsumer
Diagnostic consumer that forwards diagnostics along to an existing, already-initialized diagnostic co...
Definition: Diagnostic.h:1789
clang::CompilerInstance::getPreprocessorPtr
std::shared_ptr< Preprocessor > getPreprocessorPtr()
Definition: CompilerInstance.h:449
clang::NamedDecl
This represents a decl that may have a name.
Definition: Decl.h:249
FrontendAction.h
clang::CompilerInstance::getAnalyzerOpts
AnalyzerOptionsRef getAnalyzerOpts()
Definition: CompilerInstance.h:252
clang::InputKind
The kind of a file that we've been handed as an input.
Definition: FrontendOptions.h:145
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:626
clang::FrontendOptions::Inputs
SmallVector< FrontendInputFile, 0 > Inputs
The input files and their types.
Definition: FrontendOptions.h:386
Preprocessor.h
Decl.h
DeclObjC.h
clang::CompilerInstance::getFileManager
FileManager & getFileManager() const
Return the current file manager to the caller.
Definition: CompilerInstance.h:404
clang::FrontendOptions
FrontendOptions - Options for controlling the behavior of the frontend.
Definition: FrontendOptions.h:228
IdentifierTable.h
clang::ento::ModelInjector::ModelInjector
ModelInjector(CompilerInstance &CI)
Definition: ModelInjector.cpp:29
llvm::SmallString< 128 >
clang::CompilerInstance::getASTContext
ASTContext & getASTContext() const
Definition: CompilerInstance.h:464
clang::CompilerInstance
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Definition: CompilerInstance.h:74
clang::CompilerInstance::getPCHContainerOperations
std::shared_ptr< PCHContainerOperations > getPCHContainerOperations() const
Definition: CompilerInstance.h:524
clang::CompilerInstance::getDiagnosticClient
DiagnosticConsumer & getDiagnosticClient() const
Definition: CompilerInstance.h:344
ModelInjector.h
LangStandard.h
clang::Language::CXX
@ CXX
clang::CompilerInstance::getInvocation
CompilerInvocation & getInvocation()
Definition: CompilerInstance.h:231
FrontendActions.h
ASTUnit.h
clang::DesiredStackSize
constexpr size_t DesiredStackSize
The amount of stack space that Clang would like to be provided with.
Definition: Stack.h:26
clang::ObjCMethodDecl
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:139
clang
Definition: CalledOnceCheck.h:17
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:69
ASTReader.h
clang::FileID
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Definition: SourceLocation.h:38
clang::ento::ModelInjector::getBody
Stmt * getBody(const FunctionDecl *D) override
CompilerInstance.h
clang::FrontendOptions::DisableFree
unsigned DisableFree
Disable memory freeing on exit.
Definition: FrontendOptions.h:231
clang::CompilerInstance::getSourceManager
SourceManager & getSourceManager() const
Return the current source manager.
Definition: CompilerInstance.h:424
SM
#define SM(sm)
Definition: Cuda.cpp:81
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1857
llvm::IntrusiveRefCntPtr< AnalyzerOptions >
Stack.h
clang::NamedDecl::getName
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:276