ModelInjector.h 2.6 KB

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