blob: cdb1ed9b3815b88b8eb6b81a7c98b5e2cd38a267 [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 <utility>
Ted Kremenekeeccb302014-08-27 15:14:15 +000023
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(D->getName()) != 0)
44 return;
45
46 SourceManager &SM = CI.getSourceManager();
47 FileID mainFileID = SM.getMainFileID();
48
49 AnalyzerOptionsRef analyzerOpts = CI.getAnalyzerOpts();
50 llvm::StringRef modelPath = analyzerOpts->Config["model-path"];
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
David Blaikieea4395e2017-01-06 19:49:01 +000065 auto Invocation = std::make_shared<CompilerInvocation>(CI.getInvocation());
Ted Kremenekeeccb302014-08-27 15:14:15 +000066
67 FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
Richard Smith40c0efa2017-04-26 18:57:40 +000068 InputKind IK = InputKind::CXX; // FIXME
Ted Kremenekeeccb302014-08-27 15:14:15 +000069 FrontendOpts.Inputs.clear();
Benjamin Kramer3204b152015-05-29 19:42:19 +000070 FrontendOpts.Inputs.emplace_back(fileName, IK);
Ted Kremenekeeccb302014-08-27 15:14:15 +000071 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
Adrian Prantlbb165fb2015-06-20 18:53:08 +000077 CompilerInstance Instance(CI.getPCHContainerOperations());
David Blaikieea4395e2017-01-06 19:49:01 +000078 Instance.setInvocation(std::move(Invocation));
Ted Kremenekeeccb302014-08-27 15:14:15 +000079 Instance.createDiagnostics(
80 new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
81 /*ShouldOwnClient=*/true);
82
83 Instance.getDiagnostics().setSourceManager(&SM);
84
85 Instance.setVirtualFileSystem(&CI.getVirtualFileSystem());
86
87 // The instance wants to take ownership, however DisableFree frontend option
88 // is set to true to avoid double free issues
89 Instance.setFileManager(&CI.getFileManager());
90 Instance.setSourceManager(&SM);
David Blaikie41565462017-01-05 19:48:07 +000091 Instance.setPreprocessor(CI.getPreprocessorPtr());
Ted Kremenekeeccb302014-08-27 15:14:15 +000092 Instance.setASTContext(&CI.getASTContext());
93
94 Instance.getPreprocessor().InitializeForModelFile();
95
96 ParseModelFileAction parseModelFile(Bodies);
97
98 const unsigned ThreadStackSize = 8 << 20;
99 llvm::CrashRecoveryContext CRC;
100
101 CRC.RunSafelyOnThread([&]() { Instance.ExecuteAction(parseModelFile); },
102 ThreadStackSize);
103
104 Instance.getPreprocessor().FinalizeForModelFile();
105
106 Instance.resetAndLeakSourceManager();
107 Instance.resetAndLeakFileManager();
108 Instance.resetAndLeakPreprocessor();
109
110 // The preprocessor enters to the main file id when parsing is started, so
111 // the main file id is changed to the model file during parsing and it needs
112 // to be reseted to the former main file id after parsing of the model file
113 // is done.
114 SM.setMainFileID(mainFileID);
115}