blob: ee2c3f513cdf28b224a59e944993c5b3b4c5b30f [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"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000011#include "clang/AST/Decl.h"
12#include "clang/Basic/IdentifierTable.h"
Ted Kremenekeeccb302014-08-27 15:14:15 +000013#include "clang/Frontend/ASTUnit.h"
14#include "clang/Frontend/CompilerInstance.h"
15#include "clang/Frontend/FrontendAction.h"
Ted Kremenekeeccb302014-08-27 15:14:15 +000016#include "clang/Lex/Preprocessor.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000017#include "clang/Serialization/ASTReader.h"
18#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
19#include "llvm/ADT/STLExtras.h"
Ted Kremenekeeccb302014-08-27 15:14:15 +000020#include "llvm/Support/CrashRecoveryContext.h"
21#include "llvm/Support/FileSystem.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000022#include <string>
23#include <utility>
Ted Kremenekeeccb302014-08-27 15:14:15 +000024
25using namespace clang;
26using namespace ento;
27
28ModelInjector::ModelInjector(CompilerInstance &CI) : CI(CI) {}
29
30Stmt *ModelInjector::getBody(const FunctionDecl *D) {
31 onBodySynthesis(D);
32 return Bodies[D->getName()];
33}
34
35Stmt *ModelInjector::getBody(const ObjCMethodDecl *D) {
36 onBodySynthesis(D);
37 return Bodies[D->getName()];
38}
39
40void ModelInjector::onBodySynthesis(const NamedDecl *D) {
41
42 // FIXME: what about overloads? Declarations can be used as keys but what
43 // about file name index? Mangled names may not be suitable for that either.
44 if (Bodies.count(D->getName()) != 0)
45 return;
46
47 SourceManager &SM = CI.getSourceManager();
48 FileID mainFileID = SM.getMainFileID();
49
50 AnalyzerOptionsRef analyzerOpts = CI.getAnalyzerOpts();
51 llvm::StringRef modelPath = analyzerOpts->Config["model-path"];
52
53 llvm::SmallString<128> fileName;
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 IntrusiveRefCntPtr<CompilerInvocation> Invocation(
67 new CompilerInvocation(CI.getInvocation()));
68
69 FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
70 InputKind IK = IK_CXX; // FIXME
71 FrontendOpts.Inputs.clear();
Benjamin Kramer3204b152015-05-29 19:42:19 +000072 FrontendOpts.Inputs.emplace_back(fileName, IK);
Ted Kremenekeeccb302014-08-27 15:14:15 +000073 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
Adrian Prantlbb165fb2015-06-20 18:53:08 +000079 CompilerInstance Instance(CI.getPCHContainerOperations());
Ted Kremenekeeccb302014-08-27 15:14:15 +000080 Instance.setInvocation(&*Invocation);
81 Instance.createDiagnostics(
82 new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
83 /*ShouldOwnClient=*/true);
84
85 Instance.getDiagnostics().setSourceManager(&SM);
86
87 Instance.setVirtualFileSystem(&CI.getVirtualFileSystem());
88
89 // The instance wants to take ownership, however DisableFree frontend option
90 // is set to true to avoid double free issues
91 Instance.setFileManager(&CI.getFileManager());
92 Instance.setSourceManager(&SM);
93 Instance.setPreprocessor(&CI.getPreprocessor());
94 Instance.setASTContext(&CI.getASTContext());
95
96 Instance.getPreprocessor().InitializeForModelFile();
97
98 ParseModelFileAction parseModelFile(Bodies);
99
100 const unsigned ThreadStackSize = 8 << 20;
101 llvm::CrashRecoveryContext CRC;
102
103 CRC.RunSafelyOnThread([&]() { Instance.ExecuteAction(parseModelFile); },
104 ThreadStackSize);
105
106 Instance.getPreprocessor().FinalizeForModelFile();
107
108 Instance.resetAndLeakSourceManager();
109 Instance.resetAndLeakFileManager();
110 Instance.resetAndLeakPreprocessor();
111
112 // The preprocessor enters to the main file id when parsing is started, so
113 // the main file id is changed to the model file during parsing and it needs
114 // to be reseted to the former main file id after parsing of the model file
115 // is done.
116 SM.setMainFileID(mainFileID);
117}