clang API Documentation

ModelInjector.cpp
Go to the documentation of this file.
00001 //===-- ModelInjector.cpp ---------------------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "ModelInjector.h"
00011 
00012 #include <string>
00013 #include <utility>
00014 
00015 #include "clang/Frontend/ASTUnit.h"
00016 #include "clang/Frontend/CompilerInstance.h"
00017 #include "clang/Frontend/FrontendAction.h"
00018 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
00019 #include "clang/Serialization/ASTReader.h"
00020 #include "clang/Basic/IdentifierTable.h"
00021 #include "clang/AST/Decl.h"
00022 #include "clang/Lex/Preprocessor.h"
00023 #include "llvm/Support/CrashRecoveryContext.h"
00024 #include "llvm/Support/FileSystem.h"
00025 #include "llvm/ADT/STLExtras.h"
00026 
00027 using namespace clang;
00028 using namespace ento;
00029 
00030 ModelInjector::ModelInjector(CompilerInstance &CI) : CI(CI) {}
00031 
00032 Stmt *ModelInjector::getBody(const FunctionDecl *D) {
00033   onBodySynthesis(D);
00034   return Bodies[D->getName()];
00035 }
00036 
00037 Stmt *ModelInjector::getBody(const ObjCMethodDecl *D) {
00038   onBodySynthesis(D);
00039   return Bodies[D->getName()];
00040 }
00041 
00042 void ModelInjector::onBodySynthesis(const NamedDecl *D) {
00043 
00044   // FIXME: what about overloads? Declarations can be used as keys but what
00045   // about file name index? Mangled names may not be suitable for that either.
00046   if (Bodies.count(D->getName()) != 0)
00047     return;
00048 
00049   SourceManager &SM = CI.getSourceManager();
00050   FileID mainFileID = SM.getMainFileID();
00051 
00052   AnalyzerOptionsRef analyzerOpts = CI.getAnalyzerOpts();
00053   llvm::StringRef modelPath = analyzerOpts->Config["model-path"];
00054 
00055   llvm::SmallString<128> fileName;
00056 
00057   if (!modelPath.empty())
00058     fileName =
00059         llvm::StringRef(modelPath.str() + "/" + D->getName().str() + ".model");
00060   else
00061     fileName = llvm::StringRef(D->getName().str() + ".model");
00062 
00063   if (!llvm::sys::fs::exists(fileName.str())) {
00064     Bodies[D->getName()] = nullptr;
00065     return;
00066   }
00067 
00068   IntrusiveRefCntPtr<CompilerInvocation> Invocation(
00069       new CompilerInvocation(CI.getInvocation()));
00070 
00071   FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
00072   InputKind IK = IK_CXX; // FIXME
00073   FrontendOpts.Inputs.clear();
00074   FrontendOpts.Inputs.push_back(FrontendInputFile(fileName, IK));
00075   FrontendOpts.DisableFree = true;
00076 
00077   Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
00078 
00079   // Modules are parsed by a separate CompilerInstance, so this code mimics that
00080   // behavior for models
00081   CompilerInstance Instance;
00082   Instance.setInvocation(&*Invocation);
00083   Instance.createDiagnostics(
00084       new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
00085       /*ShouldOwnClient=*/true);
00086 
00087   Instance.getDiagnostics().setSourceManager(&SM);
00088 
00089   Instance.setVirtualFileSystem(&CI.getVirtualFileSystem());
00090 
00091   // The instance wants to take ownership, however DisableFree frontend option
00092   // is set to true to avoid double free issues
00093   Instance.setFileManager(&CI.getFileManager());
00094   Instance.setSourceManager(&SM);
00095   Instance.setPreprocessor(&CI.getPreprocessor());
00096   Instance.setASTContext(&CI.getASTContext());
00097 
00098   Instance.getPreprocessor().InitializeForModelFile();
00099 
00100   ParseModelFileAction parseModelFile(Bodies);
00101 
00102   const unsigned ThreadStackSize = 8 << 20;
00103   llvm::CrashRecoveryContext CRC;
00104 
00105   CRC.RunSafelyOnThread([&]() { Instance.ExecuteAction(parseModelFile); },
00106                         ThreadStackSize);
00107 
00108   Instance.getPreprocessor().FinalizeForModelFile();
00109 
00110   Instance.resetAndLeakSourceManager();
00111   Instance.resetAndLeakFileManager();
00112   Instance.resetAndLeakPreprocessor();
00113 
00114   // The preprocessor enters to the main file id when parsing is started, so
00115   // the main file id is changed to the model file during parsing and it needs
00116   // to be reseted to the former main file id after parsing of the model file
00117   // is done.
00118   SM.setMainFileID(mainFileID);
00119 }