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