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"
11#include "clang/AST/DeclObjC.h"
12#include "clang/Basic/LangStandard.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/Support/CrashRecoveryContext.h"
21#include "llvm/Support/FileSystem.h"
22#include <utility>
23
24using namespace clang;
25using namespace ento;
26
27ModelInjector::ModelInjector(CompilerInstance &CI) : CI(CI) {}
28
29Stmt *ModelInjector::getBody(const FunctionDecl *D) {
30 onBodySynthesis(D);
31 return Bodies[D->getName()];
32}
33
34Stmt *ModelInjector::getBody(const ObjCMethodDecl *D) {
35 onBodySynthesis(D);
36 return Bodies[D->getName()];
37}
38
39void 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(Key: D->getName()) != 0)
44 return;
45
46 SourceManager &SM = CI.getSourceManager();
47 FileID mainFileID = SM.getMainFileID();
48
49 llvm::StringRef modelPath = CI.getAnalyzerOpts().ModelPath;
50
51 llvm::SmallString<128> fileName;
52
53 if (!modelPath.empty())
54 fileName =
55 llvm::StringRef(modelPath.str() + "/" + D->getName().str() + ".model");
56 else
57 fileName = llvm::StringRef(D->getName().str() + ".model");
58
59 if (!llvm::sys::fs::exists(Path: fileName.str())) {
60 Bodies[D->getName()] = nullptr;
61 return;
62 }
63
64 auto Invocation = std::make_shared<CompilerInvocation>(args&: CI.getInvocation());
65
66 FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
67 InputKind IK = Language::CXX; // FIXME
68 FrontendOpts.Inputs.clear();
69 FrontendOpts.Inputs.emplace_back(Args&: fileName, Args&: IK);
70 FrontendOpts.DisableFree = true;
71
72 Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
73
74 // Modules are parsed by a separate CompilerInstance, so this code mimics that
75 // behavior for models
76 CompilerInstance Instance(std::move(Invocation),
77 CI.getPCHContainerOperations());
78 Instance.createDiagnostics(
79 VFS&: CI.getVirtualFileSystem(),
80 Client: new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
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(Act&: parseModelFile); },
99 RequestedStackSize: DesiredStackSize);
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}
113