Change ASTUnit to take the Diagnostic as an argument, the client should have control of this.

llvm-svn: 82430
This commit is contained in:
Daniel Dunbar 2009-09-21 03:03:39 +00:00
parent 948062a592
commit 7cd285f0fe
6 changed files with 36 additions and 35 deletions

View File

@ -14,6 +14,7 @@
#ifndef LLVM_CLANG_FRONTEND_ASTUNIT_H
#define LLVM_CLANG_FRONTEND_ASTUNIT_H
#include "clang/Basic/SourceManager.h"
#include "llvm/ADT/OwningPtr.h"
#include <string>
@ -21,7 +22,6 @@ namespace clang {
class FileManager;
class FileEntry;
class SourceManager;
class DiagnosticClient;
class Diagnostic;
class HeaderSearch;
class TargetInfo;
@ -32,23 +32,22 @@ namespace clang {
/// \brief Utility class for loading a ASTContext from a PCH file.
///
class ASTUnit {
llvm::OwningPtr<SourceManager> SourceMgr;
llvm::OwningPtr<DiagnosticClient> DiagClient;
llvm::OwningPtr<Diagnostic> Diags;
Diagnostic &Diags;
SourceManager SourceMgr;
llvm::OwningPtr<HeaderSearch> HeaderInfo;
llvm::OwningPtr<TargetInfo> Target;
llvm::OwningPtr<Preprocessor> PP;
llvm::OwningPtr<ASTContext> Ctx;
ASTUnit(const ASTUnit&); // do not implement
ASTUnit &operator=(const ASTUnit &); // do not implement
ASTUnit();
ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
ASTUnit(Diagnostic &_Diag);
public:
~ASTUnit();
const SourceManager &getSourceManager() const { return *SourceMgr.get(); }
SourceManager &getSourceManager() { return *SourceMgr.get(); }
const SourceManager &getSourceManager() const { return SourceMgr; }
SourceManager &getSourceManager() { return SourceMgr; }
const Preprocessor &getPreprocessor() const { return *PP.get(); }
Preprocessor &getPreprocessor() { return *PP.get(); }
@ -56,22 +55,26 @@ public:
const ASTContext &getASTContext() const { return *Ctx.get(); }
ASTContext &getASTContext() { return *Ctx.get(); }
const Diagnostic &getDiagnostic() const { return *Diags.get(); }
Diagnostic &getDiagnostic() { return *Diags.get(); }
const Diagnostic &getDiagnostic() const { return Diags; }
Diagnostic &getDiagnostic() { return Diags; }
FileManager &getFileManager();
const std::string &getOriginalSourceFileName();
/// \brief Create a ASTUnit from a PCH file.
///
/// \param Filename PCH filename
/// \param Filename - The PCH file to load.
///
/// \param FileMgr The FileManager to use
/// \param Diags - The Diagnostic implementation to use.
///
/// \param ErrMsg Error message to report if the PCH file could not be loaded
/// \param FileMgr - The FileManager to use.
///
/// \returns the initialized ASTUnit or NULL if the PCH failed to load
/// \param ErrMsg - Error message to report if the PCH file could not be
/// loaded.
///
/// \returns - The initialized ASTUnit or null if the PCH failed to load.
static ASTUnit *LoadFromPCHFile(const std::string &Filename,
Diagnostic &Diags,
FileManager &FileMgr,
std::string *ErrMsg = 0);
};

View File

@ -13,7 +13,6 @@
#include "clang/Frontend/ASTUnit.h"
#include "clang/Frontend/PCHReader.h"
#include "clang/Frontend/TextDiagnosticBuffer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclVisitor.h"
#include "clang/AST/StmtVisitor.h"
@ -25,7 +24,7 @@
using namespace clang;
ASTUnit::ASTUnit() { }
ASTUnit::ASTUnit(Diagnostic &_Diags) : Diags(_Diags) { }
ASTUnit::~ASTUnit() { }
namespace {
@ -86,19 +85,12 @@ FileManager &ASTUnit::getFileManager() {
}
ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
Diagnostic &Diags,
FileManager &FileMgr,
std::string *ErrMsg) {
llvm::OwningPtr<ASTUnit> AST(new ASTUnit());
AST->DiagClient.reset(new TextDiagnosticBuffer());
AST->Diags.reset(new Diagnostic(AST->DiagClient.get()));
llvm::OwningPtr<ASTUnit> AST(new ASTUnit(Diags));
AST->HeaderInfo.reset(new HeaderSearch(FileMgr));
AST->SourceMgr.reset(new SourceManager());
Diagnostic &Diags = *AST->Diags.get();
SourceManager &SourceMgr = *AST->SourceMgr.get();
// Gather Info for preprocessor construction later on.
@ -111,7 +103,7 @@ ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
llvm::OwningPtr<PCHReader> Reader;
llvm::OwningPtr<ExternalASTSource> Source;
Reader.reset(new PCHReader(SourceMgr, FileMgr, Diags));
Reader.reset(new PCHReader(AST->getSourceManager(), FileMgr, AST->Diags));
Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
Predefines, Counter));
@ -130,8 +122,8 @@ ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
// Get information about the target being compiled for.
AST->Target.reset(TargetInfo::CreateTargetInfo(TargetTriple));
AST->PP.reset(new Preprocessor(Diags, LangInfo, *AST->Target.get(),
SourceMgr, HeaderInfo));
AST->PP.reset(new Preprocessor(AST->Diags, LangInfo, *AST->Target.get(),
AST->getSourceManager(), HeaderInfo));
Preprocessor &PP = *AST->PP.get();
PP.setPredefines(Predefines);
@ -141,7 +133,7 @@ ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
// Create and initialize the ASTContext.
AST->Ctx.reset(new ASTContext(LangInfo,
SourceMgr,
AST->getSourceManager(),
*AST->Target.get(),
PP.getIdentifierTable(),
PP.getSelectorTable(),

View File

@ -206,7 +206,8 @@ CXTranslationUnit clang_createTranslationUnit(
std::string astName(ast_filename);
std::string ErrMsg;
return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getFileManager(), &ErrMsg);
return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(),
CXXIdx->getFileManager(), &ErrMsg);
}
void clang_disposeTranslationUnit(

View File

@ -2161,7 +2161,7 @@ static void ProcessASTInputFile(const std::string &InFile, ProgActions PA,
// FIXME: This is manufactoring its own diags and source manager, we should
// reuse ours.
std::string Error;
llvm::OwningPtr<ASTUnit> AST(ASTUnit::LoadFromPCHFile(InFile, FileMgr,
llvm::OwningPtr<ASTUnit> AST(ASTUnit::LoadFromPCHFile(InFile, Diags, FileMgr,
&Error));
if (!AST) {
Diags.Report(FullSourceLoc(), diag::err_fe_invalid_ast_file) << Error;

View File

@ -225,8 +225,8 @@ int main(int argc, char **argv) {
std::string ErrMsg;
llvm::OwningPtr<ASTUnit> AST;
AST.reset(ASTUnit::LoadFromPCHFile(InFile, Idxer.getFileManager(),
&ErrMsg));
AST.reset(ASTUnit::LoadFromPCHFile(InFile, Idxer.getDiagnostics(),
Idxer.getFileManager(), &ErrMsg));
if (!AST) {
llvm::errs() << "[" << InFile << "] Error: " << ErrMsg << '\n';
return 1;

View File

@ -15,6 +15,8 @@
#include "clang/Analysis/CallGraph.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/TextDiagnosticBuffer.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
@ -31,13 +33,16 @@ int main(int argc, char **argv) {
if (InputFilenames.empty())
return 0;
TextDiagnosticBuffer DiagClient;
Diagnostic Diags(&DiagClient);
for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
const std::string &InFile = InputFilenames[i];
std::string ErrMsg;
llvm::OwningPtr<ASTUnit> AST;
AST.reset(ASTUnit::LoadFromPCHFile(InFile, FileMgr, &ErrMsg));
AST.reset(ASTUnit::LoadFromPCHFile(InFile, Diags, FileMgr, &ErrMsg));
if (!AST) {
llvm::errs() << "[" << InFile << "] error: " << ErrMsg << '\n';