Path: blob/main/contrib/llvm-project/clang/lib/StaticAnalyzer/Frontend/ModelInjector.cpp
35266 views
//===-- ModelInjector.cpp ---------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "ModelInjector.h"9#include "clang/AST/Decl.h"10#include "clang/Basic/IdentifierTable.h"11#include "clang/Basic/LangStandard.h"12#include "clang/Basic/Stack.h"13#include "clang/AST/DeclObjC.h"14#include "clang/Frontend/ASTUnit.h"15#include "clang/Frontend/CompilerInstance.h"16#include "clang/Frontend/FrontendAction.h"17#include "clang/Lex/Preprocessor.h"18#include "clang/Serialization/ASTReader.h"19#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"20#include "llvm/ADT/STLExtras.h"21#include "llvm/Support/CrashRecoveryContext.h"22#include "llvm/Support/FileSystem.h"23#include <utility>2425using namespace clang;26using namespace ento;2728ModelInjector::ModelInjector(CompilerInstance &CI) : CI(CI) {}2930Stmt *ModelInjector::getBody(const FunctionDecl *D) {31onBodySynthesis(D);32return Bodies[D->getName()];33}3435Stmt *ModelInjector::getBody(const ObjCMethodDecl *D) {36onBodySynthesis(D);37return Bodies[D->getName()];38}3940void ModelInjector::onBodySynthesis(const NamedDecl *D) {4142// FIXME: what about overloads? Declarations can be used as keys but what43// about file name index? Mangled names may not be suitable for that either.44if (Bodies.count(D->getName()) != 0)45return;4647SourceManager &SM = CI.getSourceManager();48FileID mainFileID = SM.getMainFileID();4950llvm::StringRef modelPath = CI.getAnalyzerOpts().ModelPath;5152llvm::SmallString<128> fileName;5354if (!modelPath.empty())55fileName =56llvm::StringRef(modelPath.str() + "/" + D->getName().str() + ".model");57else58fileName = llvm::StringRef(D->getName().str() + ".model");5960if (!llvm::sys::fs::exists(fileName.str())) {61Bodies[D->getName()] = nullptr;62return;63}6465auto Invocation = std::make_shared<CompilerInvocation>(CI.getInvocation());6667FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();68InputKind IK = Language::CXX; // FIXME69FrontendOpts.Inputs.clear();70FrontendOpts.Inputs.emplace_back(fileName, IK);71FrontendOpts.DisableFree = true;7273Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;7475// Modules are parsed by a separate CompilerInstance, so this code mimics that76// behavior for models77CompilerInstance Instance(CI.getPCHContainerOperations());78Instance.setInvocation(std::move(Invocation));79Instance.createDiagnostics(80new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),81/*ShouldOwnClient=*/true);8283Instance.getDiagnostics().setSourceManager(&SM);8485// The instance wants to take ownership, however DisableFree frontend option86// is set to true to avoid double free issues87Instance.setFileManager(&CI.getFileManager());88Instance.setSourceManager(&SM);89Instance.setPreprocessor(CI.getPreprocessorPtr());90Instance.setASTContext(&CI.getASTContext());9192Instance.getPreprocessor().InitializeForModelFile();9394ParseModelFileAction parseModelFile(Bodies);9596llvm::CrashRecoveryContext CRC;9798CRC.RunSafelyOnThread([&]() { Instance.ExecuteAction(parseModelFile); },99DesiredStackSize);100101Instance.getPreprocessor().FinalizeForModelFile();102103Instance.resetAndLeakSourceManager();104Instance.resetAndLeakFileManager();105Instance.resetAndLeakPreprocessor();106107// The preprocessor enters to the main file id when parsing is started, so108// the main file id is changed to the model file during parsing and it needs109// to be reset to the former main file id after parsing of the model file110// is done.111SM.setMainFileID(mainFileID);112}113114115