Hi everybody,
I am new to clang and I am trying to learn it going through this tutorial: http://www.cs.rpi.edu/~laprej/clang.html
I have a very basic problem though. After some work I ended up to this piece of code:
int main(int argc, char** argv)
{
FileManager* fileManager = new FileManager();
LangOptions* languageOptions = new LangOptions();
languageOptions->BCPLComment=1;
languageOptions->C99=1;
DiagnosticOptions* diagnosticOptions=new DiagnosticOptions();
llvm::raw_os_ostream os(cout);
TextDiagnosticPrinter* diagnosticClient=new TextDiagnosticPrinter(os,diagnosticOptions,true);
Diagnostic diagnostic = new Diagnostic(diagnosticClient);
SourceManager* sourceManager= new SourceManager(diagnostic);
HeaderSearch headerSearch = new HeaderSearch(*fileManager);
TargetOptions targetOptions;
targetOptions.Triple=LLVM_HOSTTRIPLE;
TargetInfo *targetInfo = TargetInfo::CreateTargetInfo (diagnostic, targetOptions);
Preprocessor preprocessor = new Preprocessor(*diagnostic, *languageOptions, *targetInfo, *sourceManager, headerSearch);
const FileEntry fileEntry=fileManager->getFile(“test.c”);
sourceManager->createMainFileID(fileEntry);
preprocessor->EnterMainSourceFile();
Token token;
do
{
preprocessor->Lex(token);
if(diagnostic->hasErrorOccurred())
{
break;
}
preprocessor->DumpToken(token);
std::cerr << std::endl;
}
while(token.isNot(tok::eof));
diagnosticClient->EndSourceFile();
return 0;
}
This works well with input files with not errors or no warnings.
Then I give as input this very minimalistic file (test.c):
int main() {
/* Nested /* comments */
}
The parsing of this code should produce a warning for the presence of the nested comment, but instead I get this output:
int ‘int’
identifier ‘main’
l_paren ‘(’
r_paren ‘)’
l_brace ‘{’
clangExample: TextDiagnosticPrinter.cpp:298: void clang::TextDiagnosticPrinter::EmitCaretDiagnostic(clang::SourceLocation, clang::CharSourceRange*, unsigned int, const clang::SourceManager&, const clang::FixItHint*, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int): Assertion `LangOpts && “Unexpected diagnostic outside source file processing”’ failed.
The problem is that the preprocessor tries to output the diagnostic message outside the right context. I also tried to call the diagnosticClient->BeginSourceFile function before the parsing of the file but no warning is displayed either, the parsing is stopped with no messages at all.
I also tried the CompilerInstance/CompilerInvocation approach with the same result.
How can I fix this issue? Where am I wrong ?
Any help is greatly appreciated.
Thank you in advance
Alberto