From b3d3e8987b11b2fe53f72ea4fc9e8c9fadc18d12 Mon Sep 17 00:00:00 2001 From: Matt Pharr Date: Fri, 23 Sep 2011 15:50:18 -0700 Subject: [PATCH] Provide a properly initialized TextDiagnosticPrinter to clang's preprocessor. Fixes issue #100 (crash when the preprocessor was trying to emit a diagnostic about a mismatched #if/#endif). --- module.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/module.cpp b/module.cpp index 689594e0..068bbaee 100644 --- a/module.cpp +++ b/module.cpp @@ -78,6 +78,7 @@ #include #include #include +#include #include #include #include @@ -1388,23 +1389,26 @@ Module::execPreprocessor(const char* infilename, llvm::raw_string_ostream* ostre std::string error; inst.createFileManager(); - inst.createDiagnostics(0, NULL); - clang::TargetOptions& options = inst.getTargetOpts(); + llvm::raw_fd_ostream stderrRaw(2, false); + clang::TextDiagnosticPrinter *diagPrinter = + new clang::TextDiagnosticPrinter(stderrRaw, clang::DiagnosticOptions()); + inst.createDiagnostics(0, NULL, diagPrinter); + + clang::TargetOptions &options = inst.getTargetOpts(); llvm::Triple triple(module->getTargetTriple()); if (triple.getTriple().empty()) triple.setTriple(llvm::sys::getHostTriple()); - options.Triple = triple.getTriple(); - clang::TargetInfo* target - = clang::TargetInfo::CreateTargetInfo(inst.getDiagnostics(), options); + clang::TargetInfo *target = + clang::TargetInfo::CreateTargetInfo(inst.getDiagnostics(), options); inst.setTarget(target); inst.createSourceManager(inst.getFileManager()); inst.InitializeSourceManager(infilename); - clang::PreprocessorOptions& opts = inst.getPreprocessorOpts(); + clang::PreprocessorOptions &opts = inst.getPreprocessorOpts(); //Add defs for ISPC and PI opts.addMacroDef("ISPC"); @@ -1417,7 +1421,10 @@ Module::execPreprocessor(const char* infilename, llvm::raw_string_ostream* ostre } } inst.createPreprocessor(); + + clang::LangOptions langOptions; + diagPrinter->BeginSourceFile(langOptions, &inst.getPreprocessor()); clang::DoPrintPreprocessedInput(inst.getPreprocessor(), ostream, inst.getPreprocessorOutputOpts()); + diagPrinter->EndSourceFile(); } -