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