ModelInjector.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/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 = InputKind::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. Instance.setVirtualFileSystem(&CI.getVirtualFileSystem());
  69. // The instance wants to take ownership, however DisableFree frontend option
  70. // is set to true to avoid double free issues
  71. Instance.setFileManager(&CI.getFileManager());
  72. Instance.setSourceManager(&SM);
  73. Instance.setPreprocessor(CI.getPreprocessorPtr());
  74. Instance.setASTContext(&CI.getASTContext());
  75. Instance.getPreprocessor().InitializeForModelFile();
  76. ParseModelFileAction parseModelFile(Bodies);
  77. llvm::CrashRecoveryContext CRC;
  78. CRC.RunSafelyOnThread([&]() { Instance.ExecuteAction(parseModelFile); },
  79. DesiredStackSize);
  80. Instance.getPreprocessor().FinalizeForModelFile();
  81. Instance.resetAndLeakSourceManager();
  82. Instance.resetAndLeakFileManager();
  83. Instance.resetAndLeakPreprocessor();
  84. // The preprocessor enters to the main file id when parsing is started, so
  85. // the main file id is changed to the model file during parsing and it needs
  86. // to be reset to the former main file id after parsing of the model file
  87. // is done.
  88. SM.setMainFileID(mainFileID);
  89. }