[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / lib / StaticAnalyzer / Frontend / ModelInjector.cpp
blob7baae6778ebd49c29fd581756cee356bf7f84bf2
1 //===-- ModelInjector.cpp ---------------------------------------*- 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 //===----------------------------------------------------------------------===//
9 #include "ModelInjector.h"
10 #include "clang/AST/Decl.h"
11 #include "clang/Basic/IdentifierTable.h"
12 #include "clang/Basic/LangStandard.h"
13 #include "clang/Basic/Stack.h"
14 #include "clang/AST/DeclObjC.h"
15 #include "clang/Frontend/ASTUnit.h"
16 #include "clang/Frontend/CompilerInstance.h"
17 #include "clang/Frontend/FrontendAction.h"
18 #include "clang/Lex/Preprocessor.h"
19 #include "clang/Serialization/ASTReader.h"
20 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/Support/CrashRecoveryContext.h"
23 #include "llvm/Support/FileSystem.h"
24 #include <utility>
26 using namespace clang;
27 using namespace ento;
29 ModelInjector::ModelInjector(CompilerInstance &CI) : CI(CI) {}
31 Stmt *ModelInjector::getBody(const FunctionDecl *D) {
32 onBodySynthesis(D);
33 return Bodies[D->getName()];
36 Stmt *ModelInjector::getBody(const ObjCMethodDecl *D) {
37 onBodySynthesis(D);
38 return Bodies[D->getName()];
41 void ModelInjector::onBodySynthesis(const NamedDecl *D) {
43 // FIXME: what about overloads? Declarations can be used as keys but what
44 // about file name index? Mangled names may not be suitable for that either.
45 if (Bodies.count(D->getName()) != 0)
46 return;
48 SourceManager &SM = CI.getSourceManager();
49 FileID mainFileID = SM.getMainFileID();
51 AnalyzerOptionsRef analyzerOpts = CI.getAnalyzerOpts();
52 llvm::StringRef modelPath = analyzerOpts->ModelPath;
54 llvm::SmallString<128> fileName;
56 if (!modelPath.empty())
57 fileName =
58 llvm::StringRef(modelPath.str() + "/" + D->getName().str() + ".model");
59 else
60 fileName = llvm::StringRef(D->getName().str() + ".model");
62 if (!llvm::sys::fs::exists(fileName.str())) {
63 Bodies[D->getName()] = nullptr;
64 return;
67 auto Invocation = std::make_shared<CompilerInvocation>(CI.getInvocation());
69 FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
70 InputKind IK = Language::CXX; // FIXME
71 FrontendOpts.Inputs.clear();
72 FrontendOpts.Inputs.emplace_back(fileName, IK);
73 FrontendOpts.DisableFree = true;
75 Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
77 // Modules are parsed by a separate CompilerInstance, so this code mimics that
78 // behavior for models
79 CompilerInstance Instance(CI.getPCHContainerOperations());
80 Instance.setInvocation(std::move(Invocation));
81 Instance.createDiagnostics(
82 new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
83 /*ShouldOwnClient=*/true);
85 Instance.getDiagnostics().setSourceManager(&SM);
87 // The instance wants to take ownership, however DisableFree frontend option
88 // is set to true to avoid double free issues
89 Instance.setFileManager(&CI.getFileManager());
90 Instance.setSourceManager(&SM);
91 Instance.setPreprocessor(CI.getPreprocessorPtr());
92 Instance.setASTContext(&CI.getASTContext());
94 Instance.getPreprocessor().InitializeForModelFile();
96 ParseModelFileAction parseModelFile(Bodies);
98 llvm::CrashRecoveryContext CRC;
100 CRC.RunSafelyOnThread([&]() { Instance.ExecuteAction(parseModelFile); },
101 DesiredStackSize);
103 Instance.getPreprocessor().FinalizeForModelFile();
105 Instance.resetAndLeakSourceManager();
106 Instance.resetAndLeakFileManager();
107 Instance.resetAndLeakPreprocessor();
109 // The preprocessor enters to the main file id when parsing is started, so
110 // the main file id is changed to the model file during parsing and it needs
111 // to be reset to the former main file id after parsing of the model file
112 // is done.
113 SM.setMainFileID(mainFileID);