ModelInjector.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include "ModelInjector.h"
  10. #include "clang/AST/Decl.h"
  11. #include "clang/Basic/IdentifierTable.h"
  12. #include "clang/Frontend/ASTUnit.h"
  13. #include "clang/Frontend/CompilerInstance.h"
  14. #include "clang/Frontend/FrontendAction.h"
  15. #include "clang/Lex/Preprocessor.h"
  16. #include "clang/Serialization/ASTReader.h"
  17. #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/Support/CrashRecoveryContext.h"
  20. #include "llvm/Support/FileSystem.h"
  21. #include <string>
  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->Config["model-path"];
  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. IntrusiveRefCntPtr<CompilerInvocation> Invocation(
  54. new CompilerInvocation(CI.getInvocation()));
  55. FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
  56. InputKind IK = IK_CXX; // FIXME
  57. FrontendOpts.Inputs.clear();
  58. FrontendOpts.Inputs.emplace_back(fileName, IK);
  59. FrontendOpts.DisableFree = true;
  60. Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
  61. // Modules are parsed by a separate CompilerInstance, so this code mimics that
  62. // behavior for models
  63. CompilerInstance Instance(CI.getPCHContainerOperations());
  64. Instance.setInvocation(&*Invocation);
  65. Instance.createDiagnostics(
  66. new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
  67. /*ShouldOwnClient=*/true);
  68. Instance.getDiagnostics().setSourceManager(&SM);
  69. Instance.setVirtualFileSystem(&CI.getVirtualFileSystem());
  70. // The instance wants to take ownership, however DisableFree frontend option
  71. // is set to true to avoid double free issues
  72. Instance.setFileManager(&CI.getFileManager());
  73. Instance.setSourceManager(&SM);
  74. Instance.setPreprocessor(&CI.getPreprocessor());
  75. Instance.setASTContext(&CI.getASTContext());
  76. Instance.getPreprocessor().InitializeForModelFile();
  77. ParseModelFileAction parseModelFile(Bodies);
  78. const unsigned ThreadStackSize = 8 << 20;
  79. llvm::CrashRecoveryContext CRC;
  80. CRC.RunSafelyOnThread([&]() { Instance.ExecuteAction(parseModelFile); },
  81. ThreadStackSize);
  82. Instance.getPreprocessor().FinalizeForModelFile();
  83. Instance.resetAndLeakSourceManager();
  84. Instance.resetAndLeakFileManager();
  85. Instance.resetAndLeakPreprocessor();
  86. // The preprocessor enters to the main file id when parsing is started, so
  87. // the main file id is changed to the model file during parsing and it needs
  88. // to be reseted to the former main file id after parsing of the model file
  89. // is done.
  90. SM.setMainFileID(mainFileID);
  91. }