ModelInjector.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/AST/DeclObjC.h"
  14. #include "clang/Frontend/ASTUnit.h"
  15. #include "clang/Frontend/CompilerInstance.h"
  16. #include "clang/Frontend/FrontendAction.h"
  17. #include "clang/Lex/Preprocessor.h"
  18. #include "clang/Serialization/ASTReader.h"
  19. #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
  20. #include "llvm/ADT/STLExtras.h"
  21. #include "llvm/Support/CrashRecoveryContext.h"
  22. #include "llvm/Support/FileSystem.h"
  23. #include <utility>
  24. using namespace clang;
  25. using namespace ento;
  26. ModelInjector::ModelInjector(CompilerInstance &CI) : CI(CI) {}
  27. Stmt *ModelInjector::getBody(const FunctionDecl *D) {
  28. onBodySynthesis(D);
  29. return Bodies[D->getName()];
  30. }
  31. Stmt *ModelInjector::getBody(const ObjCMethodDecl *D) {
  32. onBodySynthesis(D);
  33. return Bodies[D->getName()];
  34. }
  35. void ModelInjector::onBodySynthesis(const NamedDecl *D) {
  36. // FIXME: what about overloads? Declarations can be used as keys but what
  37. // about file name index? Mangled names may not be suitable for that either.
  38. if (Bodies.count(D->getName()) != 0)
  39. return;
  40. SourceManager &SM = CI.getSourceManager();
  41. FileID mainFileID = SM.getMainFileID();
  42. AnalyzerOptionsRef analyzerOpts = CI.getAnalyzerOpts();
  43. llvm::StringRef modelPath = analyzerOpts->ModelPath;
  44. llvm::SmallString<128> fileName;
  45. if (!modelPath.empty())
  46. fileName =
  47. llvm::StringRef(modelPath.str() + "/" + D->getName().str() + ".model");
  48. else
  49. fileName = llvm::StringRef(D->getName().str() + ".model");
  50. if (!llvm::sys::fs::exists(fileName.str())) {
  51. Bodies[D->getName()] = nullptr;
  52. return;
  53. }
  54. auto Invocation = std::make_shared<CompilerInvocation>(CI.getInvocation());
  55. FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
  56. InputKind IK = Language::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(std::move(Invocation));
  65. Instance.createDiagnostics(
  66. new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
  67. /*ShouldOwnClient=*/true);
  68. Instance.getDiagnostics().setSourceManager(&SM);
  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. }