typedef std::map<FileID, std::vector<PreprocessedEntity *> >
PreprocessedEntitiesByFileMap;
private:
+ llvm::MaybeOwningPtr<Diagnostic> Diagnostics;
llvm::OwningPtr<FileManager> FileMgr;
llvm::OwningPtr<SourceManager> SourceMgr;
llvm::OwningPtr<HeaderSearch> HeaderInfo;
ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
- ASTUnit(Diagnostic &Diag, bool MainFileIsAST);
+ explicit ASTUnit(bool MainFileIsAST);
public:
class ConcurrencyCheck {
bool isMainFileAST() const { return MainFileIsAST; }
+ const Diagnostic &getDiagnostics() const { return *Diagnostics; }
+ Diagnostic &getDiagnostics() { return *Diagnostics; }
+
const SourceManager &getSourceManager() const { return *SourceMgr; }
SourceManager &getSourceManager() { return *SourceMgr; }
///
/// \returns - The initialized ASTUnit or null if the PCH failed to load.
static ASTUnit *LoadFromPCHFile(const std::string &Filename,
- Diagnostic &Diags,
+ llvm::MaybeOwningPtr<Diagnostic> Diags,
bool OnlyLocalDecls = false,
RemappedFile *RemappedFiles = 0,
unsigned NumRemappedFiles = 0,
// FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we
// shouldn't need to specify them at construction time.
static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI,
- Diagnostic &Diags,
+ llvm::MaybeOwningPtr<Diagnostic> Diags,
bool OnlyLocalDecls = false,
bool CaptureDiagnostics = false);
// shouldn't need to specify them at construction time.
static ASTUnit *LoadFromCommandLine(const char **ArgBegin,
const char **ArgEnd,
- Diagnostic &Diags,
+ llvm::MaybeOwningPtr<Diagnostic> Diags,
llvm::StringRef ResourceFilesPath,
bool OnlyLocalDecls = false,
RemappedFile *RemappedFiles = 0,
bool CaptureDiagnostics = false);
};
+/// \brief Return an potentially-owning pointer for the given diagnostic engine
+/// that owns the pointer.
+inline llvm::MaybeOwningPtr<Diagnostic> OwnedDiag(Diagnostic &Diags) {
+ return llvm::MaybeOwningPtr<Diagnostic>(&Diags, true);
+}
+
+/// \brief Return a potentially-owning pointer for the given diagnostic engine
+/// that does not own the pointer.
+inline llvm::MaybeOwningPtr<Diagnostic> UnownedDiag(Diagnostic &Diags) {
+ return llvm::MaybeOwningPtr<Diagnostic>(&Diags, false);
+}
+
+/// \brief Return an potentially-owning pointer that indicates that the
+/// default diagnostic engine should be used.
+inline llvm::MaybeOwningPtr<Diagnostic> DefaultDiag() {
+ return llvm::MaybeOwningPtr<Diagnostic>();
+}
+
} // namespace clang
#endif
#include "llvm/System/Path.h"
using namespace clang;
-ASTUnit::ASTUnit(Diagnostic &Diag, bool _MainFileIsAST)
- : MainFileIsAST(_MainFileIsAST),
- ConcurrencyCheckValue(CheckUnlocked) {
- FileMgr.reset(new FileManager);
- SourceMgr.reset(new SourceManager(Diag));
-}
+ASTUnit::ASTUnit(bool _MainFileIsAST)
+ : MainFileIsAST(_MainFileIsAST), ConcurrencyCheckValue(CheckUnlocked) { }
+
ASTUnit::~ASTUnit() {
ConcurrencyCheckValue = CheckLocked;
for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
}
ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
- Diagnostic &Diags,
+ llvm::MaybeOwningPtr<Diagnostic> Diags,
bool OnlyLocalDecls,
RemappedFile *RemappedFiles,
unsigned NumRemappedFiles,
bool CaptureDiagnostics) {
- llvm::OwningPtr<ASTUnit> AST(new ASTUnit(Diags, true));
+ llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
+
+ if (Diags.get())
+ AST->Diagnostics = Diags;
+ else {
+ // No diagnostics engine was provided, so create our own diagnostics object
+ // with the default options.
+ DiagnosticOptions DiagOpts;
+ AST->Diagnostics.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0),
+ true);
+ }
+
AST->OnlyLocalDecls = OnlyLocalDecls;
+ AST->FileMgr.reset(new FileManager);
+ AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics()));
AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
// If requested, capture diagnostics in the ASTUnit.
- CaptureDroppedDiagnostics Capture(CaptureDiagnostics, Diags,
+ CaptureDroppedDiagnostics Capture(CaptureDiagnostics, AST->getDiagnostics(),
AST->StoredDiagnostics);
for (unsigned I = 0; I != NumRemappedFiles; ++I) {
RemappedFiles[I].second->getBufferSize(),
0);
if (!FromFile) {
- Diags.Report(diag::err_fe_remap_missing_from_file)
+ AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file)
<< RemappedFiles[I].first;
delete RemappedFiles[I].second;
continue;
llvm::OwningPtr<ExternalASTSource> Source;
Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
- Diags));
+ AST->getDiagnostics()));
Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
Predefines, Counter));
case PCHReader::Failure:
case PCHReader::IgnorePCH:
- Diags.Report(diag::err_fe_unable_to_load_pch);
+ AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch);
return NULL;
}
TargetOpts.CPU = "";
TargetOpts.Features.clear();
TargetOpts.Triple = TargetTriple;
- AST->Target.reset(TargetInfo::CreateTargetInfo(Diags, TargetOpts));
- AST->PP.reset(new Preprocessor(Diags, LangInfo, *AST->Target.get(),
+ AST->Target.reset(TargetInfo::CreateTargetInfo(AST->getDiagnostics(),
+ TargetOpts));
+ AST->PP.reset(new Preprocessor(AST->getDiagnostics(), LangInfo,
+ *AST->Target.get(),
AST->getSourceManager(), HeaderInfo));
Preprocessor &PP = *AST->PP.get();
}
ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
- Diagnostic &Diags,
+ llvm::MaybeOwningPtr<Diagnostic> Diags,
bool OnlyLocalDecls,
bool CaptureDiagnostics) {
// Create the compiler instance to use for building the AST.
llvm::OwningPtr<ASTUnit> AST;
llvm::OwningPtr<TopLevelDeclTrackerAction> Act;
+ if (!Diags.get()) {
+ // No diagnostics engine was provided, so create our own diagnostics object
+ // with the default options.
+ DiagnosticOptions DiagOpts;
+ Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0), true);
+ }
+
Clang.setInvocation(CI);
- Clang.setDiagnostics(&Diags);
- Clang.setDiagnosticClient(Diags.getClient());
+ Clang.setDiagnostics(Diags.get());
+ Clang.setDiagnosticClient(Diags->getClient());
// Create the target instance.
Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
"FIXME: AST inputs not yet supported here!");
// Create the AST unit.
- AST.reset(new ASTUnit(Diags, false));
+ AST.reset(new ASTUnit(false));
+ AST->Diagnostics = Diags;
+ AST->FileMgr.reset(new FileManager);
+ AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics()));
AST->OnlyLocalDecls = OnlyLocalDecls;
AST->OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
const char **ArgEnd,
- Diagnostic &Diags,
+ llvm::MaybeOwningPtr<Diagnostic> Diags,
llvm::StringRef ResourceFilesPath,
bool OnlyLocalDecls,
RemappedFile *RemappedFiles,
unsigned NumRemappedFiles,
bool CaptureDiagnostics) {
+ if (!Diags.get()) {
+ // No diagnostics engine was provided, so create our own diagnostics object
+ // with the default options.
+ DiagnosticOptions DiagOpts;
+ Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0), true);
+ }
+
llvm::SmallVector<const char *, 16> Args;
Args.push_back("<clang>"); // FIXME: Remove dummy argument.
Args.insert(Args.end(), ArgBegin, ArgEnd);
// FIXME: We shouldn't have to pass in the path info.
driver::Driver TheDriver("clang", "/", llvm::sys::getHostTriple(),
- "a.out", false, false, Diags);
+ "a.out", false, false, *Diags);
// Don't check that inputs exist, they have been remapped.
TheDriver.setCheckInputsExist(false);
llvm::SmallString<256> Msg;
llvm::raw_svector_ostream OS(Msg);
C->PrintJob(OS, C->getJobs(), "; ", true);
- Diags.Report(diag::err_fe_expected_compiler_job) << OS.str();
+ Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
return 0;
}
const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") {
- Diags.Report(diag::err_fe_expected_clang_command);
+ Diags->Report(diag::err_fe_expected_clang_command);
return 0;
}
llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation);
CompilerInvocation::CreateFromArgs(*CI, (const char**) CCArgs.data(),
(const char**) CCArgs.data()+CCArgs.size(),
- Diags);
+ *Diags);
// Override any files that need remapping
for (unsigned I = 0; I != NumRemappedFiles; ++I)
CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
- // Configure the diagnostics.
- DiagnosticOptions DiagOpts;
- llvm::OwningPtr<Diagnostic> Diags;
- Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
- return ASTUnit::LoadFromPCHFile(ast_filename, *Diags,
+ return ASTUnit::LoadFromPCHFile(ast_filename, DefaultDiag(),
CXXIdx->getOnlyLocalDecls(),
0, 0, true);
}
// Configure the diagnostics.
DiagnosticOptions DiagOpts;
- llvm::OwningPtr<Diagnostic> Diags;
- Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
+ llvm::MaybeOwningPtr<Diagnostic> Diags;
+ Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0), true);
llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
for (unsigned I = 0; I != num_unsaved_files; ++I) {
llvm::OwningPtr<ASTUnit> Unit(
ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
- *Diags,
+ Diags,
CXXIdx->getClangResourcesPath(),
CXXIdx->getOnlyLocalDecls(),
RemappedFiles.data(),
Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg;
}
- ASTUnit *ATU = ASTUnit::LoadFromPCHFile(astTmpFile, *Diags,
+ ASTUnit *ATU = ASTUnit::LoadFromPCHFile(astTmpFile, Diags,
CXXIdx->getOnlyLocalDecls(),
RemappedFiles.data(),
RemappedFiles.size(),