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