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