blob: d5b623cd840b630a605704c840887bd97297942a [file] [log] [blame]
Ted Kremenekeeccb302014-08-27 15:14:15 +00001//===-- 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
12#include <string>
13#include <utility>
14
15#include "clang/Frontend/ASTUnit.h"
16#include "clang/Frontend/CompilerInstance.h"
17#include "clang/Frontend/FrontendAction.h"
18#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
19#include "clang/Serialization/ASTReader.h"
20#include "clang/Basic/IdentifierTable.h"
21#include "clang/AST/Decl.h"
22#include "clang/Lex/Preprocessor.h"
23#include "llvm/Support/CrashRecoveryContext.h"
24#include "llvm/Support/FileSystem.h"
25#include "llvm/ADT/STLExtras.h"
26
27using namespace clang;
28using namespace ento;
29
30ModelInjector::ModelInjector(CompilerInstance &CI) : CI(CI) {}
31
32Stmt *ModelInjector::getBody(const FunctionDecl *D) {
33 onBodySynthesis(D);
34 return Bodies[D->getName()];
35}
36
37Stmt *ModelInjector::getBody(const ObjCMethodDecl *D) {
38 onBodySynthesis(D);
39 return Bodies[D->getName()];
40}
41
42void ModelInjector::onBodySynthesis(const NamedDecl *D) {
43
44 // FIXME: what about overloads? Declarations can be used as keys but what
45 // about file name index? Mangled names may not be suitable for that either.
46 if (Bodies.count(D->getName()) != 0)
47 return;
48
49 SourceManager &SM = CI.getSourceManager();
50 FileID mainFileID = SM.getMainFileID();
51
52 AnalyzerOptionsRef analyzerOpts = CI.getAnalyzerOpts();
53 llvm::StringRef modelPath = analyzerOpts->Config["model-path"];
54
55 llvm::SmallString<128> fileName;
56
57 if (!modelPath.empty())
58 fileName =
59 llvm::StringRef(modelPath.str() + "/" + D->getName().str() + ".model");
60 else
61 fileName = llvm::StringRef(D->getName().str() + ".model");
62
63 if (!llvm::sys::fs::exists(fileName.str())) {
64 Bodies[D->getName()] = nullptr;
65 return;
66 }
67
68 IntrusiveRefCntPtr<CompilerInvocation> Invocation(
69 new CompilerInvocation(CI.getInvocation()));
70
71 FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
72 InputKind IK = IK_CXX; // FIXME
73 FrontendOpts.Inputs.clear();
74 FrontendOpts.Inputs.push_back(FrontendInputFile(fileName, IK));
75 FrontendOpts.DisableFree = true;
76
77 Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
78
79 // Modules are parsed by a separate CompilerInstance, so this code mimics that
80 // behavior for models
81 CompilerInstance Instance;
82 Instance.setInvocation(&*Invocation);
83 Instance.createDiagnostics(
84 new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
85 /*ShouldOwnClient=*/true);
86
87 Instance.getDiagnostics().setSourceManager(&SM);
88
89 Instance.setVirtualFileSystem(&CI.getVirtualFileSystem());
90
91 // The instance wants to take ownership, however DisableFree frontend option
92 // is set to true to avoid double free issues
93 Instance.setFileManager(&CI.getFileManager());
94 Instance.setSourceManager(&SM);
95 Instance.setPreprocessor(&CI.getPreprocessor());
96 Instance.setASTContext(&CI.getASTContext());
97
98 Instance.getPreprocessor().InitializeForModelFile();
99
100 ParseModelFileAction parseModelFile(Bodies);
101
102 const unsigned ThreadStackSize = 8 << 20;
103 llvm::CrashRecoveryContext CRC;
104
105 CRC.RunSafelyOnThread([&]() { Instance.ExecuteAction(parseModelFile); },
106 ThreadStackSize);
107
108 Instance.getPreprocessor().FinalizeForModelFile();
109
110 Instance.resetAndLeakSourceManager();
111 Instance.resetAndLeakFileManager();
112 Instance.resetAndLeakPreprocessor();
113
114 // The preprocessor enters to the main file id when parsing is started, so
115 // the main file id is changed to the model file during parsing and it needs
116 // to be reseted to the former main file id after parsing of the model file
117 // is done.
118 SM.setMainFileID(mainFileID);
119}