1 //===-- 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 #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" 22 #include <utility> 23 24 using namespace clang; 25 using namespace ento; 26 27 ModelInjector::ModelInjector(CompilerInstance &CI) : CI(CI) {} 28 29 Stmt *ModelInjector::getBody(const FunctionDecl *D) { 30 onBodySynthesis(D); 31 return Bodies[D->getName()]; 32 } 33 34 Stmt *ModelInjector::getBody(const ObjCMethodDecl *D) { 35 onBodySynthesis(D); 36 return Bodies[D->getName()]; 37 } 38 39 void 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 65 IntrusiveRefCntPtr<CompilerInvocation> Invocation( 66 new CompilerInvocation(CI.getInvocation())); 67 68 FrontendOptions &FrontendOpts = Invocation->getFrontendOpts(); 69 InputKind IK = IK_CXX; // FIXME 70 FrontendOpts.Inputs.clear(); 71 FrontendOpts.Inputs.emplace_back(fileName, IK); 72 FrontendOpts.DisableFree = true; 73 74 Invocation->getDiagnosticOpts().VerifyDiagnostics = 0; 75 76 // Modules are parsed by a separate CompilerInstance, so this code mimics that 77 // behavior for models 78 CompilerInstance Instance(CI.getPCHContainerOperations()); 79 Instance.setInvocation(&*Invocation); 80 Instance.createDiagnostics( 81 new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()), 82 /*ShouldOwnClient=*/true); 83 84 Instance.getDiagnostics().setSourceManager(&SM); 85 86 Instance.setVirtualFileSystem(&CI.getVirtualFileSystem()); 87 88 // The instance wants to take ownership, however DisableFree frontend option 89 // is set to true to avoid double free issues 90 Instance.setFileManager(&CI.getFileManager()); 91 Instance.setSourceManager(&SM); 92 Instance.setPreprocessor(&CI.getPreprocessor()); 93 Instance.setASTContext(&CI.getASTContext()); 94 95 Instance.getPreprocessor().InitializeForModelFile(); 96 97 ParseModelFileAction parseModelFile(Bodies); 98 99 const unsigned ThreadStackSize = 8 << 20; 100 llvm::CrashRecoveryContext CRC; 101 102 CRC.RunSafelyOnThread([&]() { Instance.ExecuteAction(parseModelFile); }, 103 ThreadStackSize); 104 105 Instance.getPreprocessor().FinalizeForModelFile(); 106 107 Instance.resetAndLeakSourceManager(); 108 Instance.resetAndLeakFileManager(); 109 Instance.resetAndLeakPreprocessor(); 110 111 // The preprocessor enters to the main file id when parsing is started, so 112 // the main file id is changed to the model file during parsing and it needs 113 // to be reseted to the former main file id after parsing of the model file 114 // is done. 115 SM.setMainFileID(mainFileID); 116 } 117