1 //===-- ModelInjector.cpp ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ModelInjector.h"
11 #include "clang/AST/Decl.h"
12 #include "clang/Basic/IdentifierTable.h"
13 #include "clang/Frontend/ASTUnit.h"
14 #include "clang/Frontend/CompilerInstance.h"
15 #include "clang/Frontend/FrontendAction.h"
16 #include "clang/Lex/Preprocessor.h"
17 #include "clang/Serialization/ASTReader.h"
18 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/CrashRecoveryContext.h"
21 #include "llvm/Support/FileSystem.h"
25 using namespace clang
;
28 ModelInjector::ModelInjector(CompilerInstance
&CI
) : CI(CI
) {}
30 Stmt
*ModelInjector::getBody(const FunctionDecl
*D
) {
32 return Bodies
[D
->getName()];
35 Stmt
*ModelInjector::getBody(const ObjCMethodDecl
*D
) {
37 return Bodies
[D
->getName()];
40 void ModelInjector::onBodySynthesis(const NamedDecl
*D
) {
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)
47 SourceManager
&SM
= CI
.getSourceManager();
48 FileID mainFileID
= SM
.getMainFileID();
50 AnalyzerOptionsRef analyzerOpts
= CI
.getAnalyzerOpts();
51 llvm::StringRef modelPath
= analyzerOpts
->Config
["model-path"];
53 llvm::SmallString
<128> fileName
;
55 if (!modelPath
.empty())
57 llvm::StringRef(modelPath
.str() + "/" + D
->getName().str() + ".model");
59 fileName
= llvm::StringRef(D
->getName().str() + ".model");
61 if (!llvm::sys::fs::exists(fileName
.str())) {
62 Bodies
[D
->getName()] = nullptr;
66 IntrusiveRefCntPtr
<CompilerInvocation
> Invocation(
67 new CompilerInvocation(CI
.getInvocation()));
69 FrontendOptions
&FrontendOpts
= Invocation
->getFrontendOpts();
70 InputKind IK
= IK_CXX
; // FIXME
71 FrontendOpts
.Inputs
.clear();
72 FrontendOpts
.Inputs
.push_back(FrontendInputFile(fileName
, IK
));
73 FrontendOpts
.DisableFree
= true;
75 Invocation
->getDiagnosticOpts().VerifyDiagnostics
= 0;
77 // Modules are parsed by a separate CompilerInstance, so this code mimics that
78 // behavior for models
79 CompilerInstance Instance
;
80 Instance
.setInvocation(&*Invocation
);
81 Instance
.createDiagnostics(
82 new ForwardingDiagnosticConsumer(CI
.getDiagnosticClient()),
83 /*ShouldOwnClient=*/true);
85 Instance
.getDiagnostics().setSourceManager(&SM
);
87 Instance
.setVirtualFileSystem(&CI
.getVirtualFileSystem());
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());
96 Instance
.getPreprocessor().InitializeForModelFile();
98 ParseModelFileAction
parseModelFile(Bodies
);
100 const unsigned ThreadStackSize
= 8 << 20;
101 llvm::CrashRecoveryContext CRC
;
103 CRC
.RunSafelyOnThread([&]() { Instance
.ExecuteAction(parseModelFile
); },
106 Instance
.getPreprocessor().FinalizeForModelFile();
108 Instance
.resetAndLeakSourceManager();
109 Instance
.resetAndLeakFileManager();
110 Instance
.resetAndLeakPreprocessor();
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
116 SM
.setMainFileID(mainFileID
);