ModelInjector.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "ModelInjector.h"
  9. #include "clang/AST/Decl.h"
  10. #include "clang/Basic/IdentifierTable.h"
  11. #include "clang/Basic/LangStandard.h"
  12. #include "clang/Basic/Stack.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. using namespace clang;
  24. using namespace ento;
  25. ModelInjector::ModelInjector(CompilerInstance &CI) : CI(CI) {}
  26. Stmt *ModelInjector::getBody(const FunctionDecl *D) {
  27. onBodySynthesis(D);
  28. return Bodies[D->getName()];
  29. }
  30. Stmt *ModelInjector::getBody(const ObjCMethodDecl *D) {
  31. onBodySynthesis(D);
  32. return Bodies[D->getName()];
  33. }
  34. void ModelInjector::onBodySynthesis(const NamedDecl *D) {
  35. // FIXME: what about overloads? Declarations can be used as keys but what
  36. // about file name index? Mangled names may not be suitable for that either.
  37. if (Bodies.count(D->getName()) != 0)
  38. return;
  39. SourceManager &SM = CI.getSourceManager();
  40. FileID mainFileID = SM.getMainFileID();
  41. AnalyzerOptionsRef analyzerOpts = CI.getAnalyzerOpts();
  42. llvm::StringRef modelPath = analyzerOpts->ModelPath;
  43. llvm::SmallString<128> fileName;
  44. if (!modelPath.empty())
  45. fileName =
  46. llvm::StringRef(modelPath.str() + "/" + D->getName().str() + ".model");
  47. else
  48. fileName = llvm::StringRef(D->getName().str() + ".model");
  49. if (!llvm::sys::fs::exists(fileName.str())) {
  50. Bodies[D->getName()] = nullptr;
  51. return;
  52. }
  53. auto Invocation = std::make_shared<CompilerInvocation>(CI.getInvocation());
  54. FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
  55. InputKind IK = Language::CXX; // FIXME
  56. FrontendOpts.Inputs.clear();
  57. FrontendOpts.Inputs.emplace_back(fileName, IK);
  58. FrontendOpts.DisableFree = true;
  59. Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
  60. // Modules are parsed by a separate CompilerInstance, so this code mimics that
  61. // behavior for models
  62. CompilerInstance Instance(CI.getPCHContainerOperations());
  63. Instance.setInvocation(std::move(Invocation));
  64. Instance.createDiagnostics(
  65. new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
  66. /*ShouldOwnClient=*/true);
  67. Instance.getDiagnostics().setSourceManager(&SM);
  68. // The instance wants to take ownership, however DisableFree frontend option
  69. // is set to true to avoid double free issues
  70. Instance.setFileManager(&CI.getFileManager());
  71. Instance.setSourceManager(&SM);
  72. Instance.setPreprocessor(CI.getPreprocessorPtr());
  73. Instance.setASTContext(&CI.getASTContext());
  74. Instance.getPreprocessor().InitializeForModelFile();
  75. ParseModelFileAction parseModelFile(Bodies);
  76. llvm::CrashRecoveryContext CRC;
  77. CRC.RunSafelyOnThread([&]() { Instance.ExecuteAction(parseModelFile); },
  78. DesiredStackSize);
  79. Instance.getPreprocessor().FinalizeForModelFile();
  80. Instance.resetAndLeakSourceManager();
  81. Instance.resetAndLeakFileManager();
  82. Instance.resetAndLeakPreprocessor();
  83. // The preprocessor enters to the main file id when parsing is started, so
  84. // the main file id is changed to the model file during parsing and it needs
  85. // to be reset to the former main file id after parsing of the model file
  86. // is done.
  87. SM.setMainFileID(mainFileID);
  88. }