ModelInjector.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //===-- ModelInjector.h -----------------------------------------*- 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. /// \file
  10. /// This file defines the clang::ento::ModelInjector class which implements the
  11. /// clang::CodeInjector interface. This class is responsible for injecting
  12. /// function definitions that were synthesized from model files.
  13. ///
  14. /// Model files allow definitions of functions to be lazily constituted for functions
  15. /// which lack bodies in the original source code. This allows the analyzer
  16. /// to more precisely analyze code that calls such functions, analyzing the
  17. /// artificial definitions (which typically approximate the semantics of the
  18. /// called function) when called by client code. These definitions are
  19. /// reconstituted lazily, on-demand, by the static analyzer engine.
  20. ///
  21. //===----------------------------------------------------------------------===//
  22. #ifndef LLVM_CLANG_SA_FRONTEND_MODELINJECTOR_H
  23. #define LLVM_CLANG_SA_FRONTEND_MODELINJECTOR_H
  24. #include "clang/Analysis/CodeInjector.h"
  25. #include "llvm/ADT/StringMap.h"
  26. namespace clang {
  27. class CompilerInstance;
  28. class ASTUnit;
  29. class ASTReader;
  30. class NamedDecl;
  31. class Module;
  32. namespace ento {
  33. class ModelInjector : public CodeInjector {
  34. public:
  35. ModelInjector(CompilerInstance &CI);
  36. Stmt *getBody(const FunctionDecl *D) override;
  37. Stmt *getBody(const ObjCMethodDecl *D) override;
  38. private:
  39. /// Synthesize a body for a declaration
  40. ///
  41. /// This method first looks up the appropriate model file based on the
  42. /// model-path configuration option and the name of the declaration that is
  43. /// looked up. If no model were synthesized yet for a function with that name
  44. /// it will create a new compiler instance to parse the model file using the
  45. /// ASTContext, Preprocessor, SourceManager of the original compiler instance.
  46. /// The former resources are shared between the two compiler instance, so the
  47. /// newly created instance have to "leak" these objects, since they are owned
  48. /// by the original instance.
  49. ///
  50. /// The model-path should be either an absolute path or relative to the
  51. /// working directory of the compiler.
  52. void onBodySynthesis(const NamedDecl *D);
  53. CompilerInstance &CI;
  54. // FIXME: double memoization is redundant, with memoization both here and in
  55. // BodyFarm.
  56. llvm::StringMap<Stmt *> Bodies;
  57. };
  58. }
  59. }
  60. #endif