| 1 | //===-- ModelInjector.cpp ---------------------------------------*- C++ -*-===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "ModelInjector.h" |
| 10 | #include "clang/AST/Decl.h" |
| 11 | #include "clang/AST/DeclObjC.h" |
| 12 | #include "clang/Basic/DiagnosticDriver.h" |
| 13 | #include "clang/Basic/LangStandard.h" |
| 14 | #include "clang/Basic/Stack.h" |
| 15 | #include "clang/Frontend/ASTUnit.h" |
| 16 | #include "clang/Frontend/CompilerInstance.h" |
| 17 | #include "clang/Frontend/FrontendAction.h" |
| 18 | #include "clang/Lex/Preprocessor.h" |
| 19 | #include "clang/Serialization/ASTReader.h" |
| 20 | #include "clang/StaticAnalyzer/Frontend/FrontendActions.h" |
| 21 | #include "llvm/Support/CrashRecoveryContext.h" |
| 22 | #include "llvm/Support/FileSystem.h" |
| 23 | #include <utility> |
| 24 | |
| 25 | using namespace clang; |
| 26 | using namespace ento; |
| 27 | |
| 28 | ModelInjector::ModelInjector(CompilerInstance &CI) : CI(CI) { |
| 29 | if (CI.getAnalyzerOpts().ShouldEmitErrorsOnInvalidConfigValue && |
| 30 | !CI.getAnalyzerOpts().ModelPath.empty()) { |
| 31 | auto S = CI.getVirtualFileSystem().status(Path: CI.getAnalyzerOpts().ModelPath); |
| 32 | if (!S || S->getType() != llvm::sys::fs::file_type::directory_file) |
| 33 | CI.getDiagnostics().Report(DiagID: diag::err_analyzer_config_invalid_input) |
| 34 | << "model-path"<< "a filename"; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | Stmt *ModelInjector::getBody(const FunctionDecl *D) { |
| 39 | onBodySynthesis(D); |
| 40 | return Bodies[D->getName()]; |
| 41 | } |
| 42 | |
| 43 | Stmt *ModelInjector::getBody(const ObjCMethodDecl *D) { |
| 44 | onBodySynthesis(D); |
| 45 | return Bodies[D->getName()]; |
| 46 | } |
| 47 | |
| 48 | void ModelInjector::onBodySynthesis(const NamedDecl *D) { |
| 49 | |
| 50 | // FIXME: what about overloads? Declarations can be used as keys but what |
| 51 | // about file name index? Mangled names may not be suitable for that either. |
| 52 | if (Bodies.count(Key: D->getName()) != 0) |
| 53 | return; |
| 54 | |
| 55 | llvm::IntrusiveRefCntPtr<SourceManager> SM = CI.getSourceManagerPtr(); |
| 56 | FileID mainFileID = SM->getMainFileID(); |
| 57 | |
| 58 | llvm::StringRef modelPath = CI.getAnalyzerOpts().ModelPath; |
| 59 | |
| 60 | llvm::SmallString<128> fileName; |
| 61 | |
| 62 | if (!modelPath.empty()) |
| 63 | fileName = |
| 64 | llvm::StringRef(modelPath.str() + "/"+ D->getName().str() + ".model"); |
| 65 | else |
| 66 | fileName = llvm::StringRef(D->getName().str() + ".model"); |
| 67 | |
| 68 | if (!CI.getVirtualFileSystem().exists(Path: fileName)) { |
| 69 | Bodies[D->getName()] = nullptr; |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | auto Invocation = std::make_shared<CompilerInvocation>(args&: CI.getInvocation()); |
| 74 | |
| 75 | FrontendOptions &FrontendOpts = Invocation->getFrontendOpts(); |
| 76 | InputKind IK = Language::CXX; // FIXME |
| 77 | FrontendOpts.Inputs.clear(); |
| 78 | FrontendOpts.Inputs.emplace_back(Args&: fileName, Args&: IK); |
| 79 | FrontendOpts.DisableFree = true; |
| 80 | |
| 81 | Invocation->getDiagnosticOpts().VerifyDiagnostics = 0; |
| 82 | |
| 83 | // Modules are parsed by a separate CompilerInstance, so this code mimics that |
| 84 | // behavior for models |
| 85 | CompilerInstance Instance(std::move(Invocation), |
| 86 | CI.getPCHContainerOperations()); |
| 87 | Instance.setVirtualFileSystem(CI.getVirtualFileSystemPtr()); |
| 88 | Instance.createDiagnostics( |
| 89 | Client: new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()), |
| 90 | /*ShouldOwnClient=*/true); |
| 91 | |
| 92 | Instance.getDiagnostics().setSourceManager(SM.get()); |
| 93 | |
| 94 | // The instance wants to take ownership, however DisableFree frontend option |
| 95 | // is set to true to avoid double free issues |
| 96 | Instance.setVirtualFileSystem(CI.getVirtualFileSystemPtr()); |
| 97 | Instance.setFileManager(CI.getFileManagerPtr()); |
| 98 | Instance.setSourceManager(SM); |
| 99 | Instance.setPreprocessor(CI.getPreprocessorPtr()); |
| 100 | Instance.setASTContext(CI.getASTContextPtr()); |
| 101 | |
| 102 | Instance.getPreprocessor().InitializeForModelFile(); |
| 103 | |
| 104 | ParseModelFileAction parseModelFile(Bodies); |
| 105 | |
| 106 | llvm::CrashRecoveryContext CRC; |
| 107 | |
| 108 | CRC.RunSafelyOnThread([&]() { Instance.ExecuteAction(Act&: parseModelFile); }, |
| 109 | RequestedStackSize: DesiredStackSize); |
| 110 | |
| 111 | Instance.getPreprocessor().FinalizeForModelFile(); |
| 112 | |
| 113 | Instance.resetAndLeakSourceManager(); |
| 114 | Instance.resetAndLeakFileManager(); |
| 115 | Instance.resetAndLeakPreprocessor(); |
| 116 | |
| 117 | // The preprocessor enters to the main file id when parsing is started, so |
| 118 | // the main file id is changed to the model file during parsing and it needs |
| 119 | // to be reset to the former main file id after parsing of the model file |
| 120 | // is done. |
| 121 | SM->setMainFileID(mainFileID); |
| 122 | } |
| 123 |