clang 19.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"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/Support/CrashRecoveryContext.h"
23#include "llvm/Support/FileSystem.h"
24#include <utility>
25
26using namespace clang;
27using namespace ento;
28
30
32 onBodySynthesis(D);
33 return Bodies[D->getName()];
34}
35
37 onBodySynthesis(D);
38 return Bodies[D->getName()];
39}
40
41void 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 llvm::StringRef modelPath = CI.getAnalyzerOpts().ModelPath;
52
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 = Language::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
79 Instance.setInvocation(std::move(Invocation));
80 Instance.createDiagnostics(
82 /*ShouldOwnClient=*/true);
83
84 Instance.getDiagnostics().setSourceManager(&SM);
85
86 // The instance wants to take ownership, however DisableFree frontend option
87 // is set to true to avoid double free issues
88 Instance.setFileManager(&CI.getFileManager());
89 Instance.setSourceManager(&SM);
90 Instance.setPreprocessor(CI.getPreprocessorPtr());
91 Instance.setASTContext(&CI.getASTContext());
92
93 Instance.getPreprocessor().InitializeForModelFile();
94
95 ParseModelFileAction parseModelFile(Bodies);
96
97 llvm::CrashRecoveryContext CRC;
98
99 CRC.RunSafelyOnThread([&]() { Instance.ExecuteAction(parseModelFile); },
101
102 Instance.getPreprocessor().FinalizeForModelFile();
103
104 Instance.resetAndLeakSourceManager();
105 Instance.resetAndLeakFileManager();
106 Instance.resetAndLeakPreprocessor();
107
108 // The preprocessor enters to the main file id when parsing is started, so
109 // the main file id is changed to the model file during parsing and it needs
110 // to be reset to the former main file id after parsing of the model file
111 // is done.
112 SM.setMainFileID(mainFileID);
113}
#define SM(sm)
Definition: Cuda.cpp:82
Defines the clang::FrontendAction interface and various convenience abstract classes (clang::ASTFront...
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
This file defines the clang::ento::ModelInjector class which implements the clang::CodeInjector inter...
Defines the clang::Preprocessor interface.
Defines utilities for dealing with stack allocation and stack space.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
DiagnosticConsumer & getDiagnosticClient() const
AnalyzerOptions & getAnalyzerOpts()
std::shared_ptr< Preprocessor > getPreprocessorPtr()
FileManager & getFileManager() const
Return the current file manager to the caller.
ASTContext & getASTContext() const
CompilerInvocation & getInvocation()
std::shared_ptr< PCHContainerOperations > getPCHContainerOperations() const
SourceManager & getSourceManager() const
Return the current source manager.
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Diagnostic consumer that forwards diagnostics along to an existing, already-initialized diagnostic co...
Definition: Diagnostic.h:1812
FrontendOptions - Options for controlling the behavior of the frontend.
unsigned DisableFree
Disable memory freeing on exit.
SmallVector< FrontendInputFile, 0 > Inputs
The input files and their types.
Represents a function declaration or definition.
Definition: Decl.h:1959
The kind of a file that we've been handed as an input.
This represents a decl that may have a name.
Definition: Decl.h:249
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:276
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:140
This class handles loading and caching of source files into memory.
Stmt - This represents one statement.
Definition: Stmt.h:84
Stmt * getBody(const FunctionDecl *D) override
ModelInjector(CompilerInstance &CI)
Frontend action to parse model files.
The JSON file list parser is used to communicate input to InstallAPI.
constexpr size_t DesiredStackSize
The amount of stack space that Clang would like to be provided with.
Definition: Stack.h:26