Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 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" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 11 | #include "clang/AST/Decl.h" |
| 12 | #include "clang/Basic/IdentifierTable.h" |
Richard Smith | 0a7b297 | 2018-07-03 21:34:13 +0000 | [diff] [blame] | 13 | #include "clang/Basic/Stack.h" |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/ASTUnit.h" |
| 15 | #include "clang/Frontend/CompilerInstance.h" |
| 16 | #include "clang/Frontend/FrontendAction.h" |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 18 | #include "clang/Serialization/ASTReader.h" |
| 19 | #include "clang/StaticAnalyzer/Frontend/FrontendActions.h" |
| 20 | #include "llvm/ADT/STLExtras.h" |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CrashRecoveryContext.h" |
| 22 | #include "llvm/Support/FileSystem.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 23 | #include <utility> |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | using namespace ento; |
| 27 | |
| 28 | ModelInjector::ModelInjector(CompilerInstance &CI) : CI(CI) {} |
| 29 | |
| 30 | Stmt *ModelInjector::getBody(const FunctionDecl *D) { |
| 31 | onBodySynthesis(D); |
| 32 | return Bodies[D->getName()]; |
| 33 | } |
| 34 | |
| 35 | Stmt *ModelInjector::getBody(const ObjCMethodDecl *D) { |
| 36 | onBodySynthesis(D); |
| 37 | return Bodies[D->getName()]; |
| 38 | } |
| 39 | |
| 40 | void 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(); |
Kristof Umann | c83b0dd | 2018-11-02 15:48:10 +0000 | [diff] [blame] | 51 | llvm::StringRef modelPath = analyzerOpts->getModelPath(); |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 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 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 66 | auto Invocation = std::make_shared<CompilerInvocation>(CI.getInvocation()); |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 67 | |
| 68 | FrontendOptions &FrontendOpts = Invocation->getFrontendOpts(); |
Richard Smith | 40c0efa | 2017-04-26 18:57:40 +0000 | [diff] [blame] | 69 | InputKind IK = InputKind::CXX; // FIXME |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 70 | FrontendOpts.Inputs.clear(); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 71 | FrontendOpts.Inputs.emplace_back(fileName, IK); |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 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 |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 78 | CompilerInstance Instance(CI.getPCHContainerOperations()); |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 79 | Instance.setInvocation(std::move(Invocation)); |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 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); |
David Blaikie | 4156546 | 2017-01-05 19:48:07 +0000 | [diff] [blame] | 92 | Instance.setPreprocessor(CI.getPreprocessorPtr()); |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 93 | Instance.setASTContext(&CI.getASTContext()); |
| 94 | |
| 95 | Instance.getPreprocessor().InitializeForModelFile(); |
| 96 | |
| 97 | ParseModelFileAction parseModelFile(Bodies); |
| 98 | |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 99 | llvm::CrashRecoveryContext CRC; |
| 100 | |
| 101 | CRC.RunSafelyOnThread([&]() { Instance.ExecuteAction(parseModelFile); }, |
Richard Smith | 0a7b297 | 2018-07-03 21:34:13 +0000 | [diff] [blame] | 102 | DesiredStackSize); |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 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 |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 112 | // to be reset to the former main file id after parsing of the model file |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 113 | // is done. |
| 114 | SM.setMainFileID(mainFileID); |
| 115 | } |