Thank you very much Eli, <br>I'll follow the code in the examples.<br><br><div class="gmail_quote">2010/9/17 Eli Friedman <span dir="ltr"><<a href="mailto:eli.friedman@gmail.com">eli.friedman@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div><div></div><div class="h5">On Thu, Sep 16, 2010 at 2:27 PM, Alberto Magni<br>
<<a href="mailto:alberto.magni86@gmail.com">alberto.magni86@gmail.com</a>> wrote:<br>
> Hi everybody,<br>
><br>
> I am new to clang and I am trying to learn it going through this tutorial:<br>
> <a href="http://www.cs.rpi.edu/%7Elaprej/clang.html" target="_blank">http://www.cs.rpi.edu/~laprej/clang.html</a><br>
> I have a very basic problem though. After some work I ended up to this<br>
> piece of code:<br>
><br>
> int main(int argc, char** argv)<br>
> {<br>
> FileManager* fileManager = new FileManager();<br>
> LangOptions* languageOptions = new LangOptions();<br>
> languageOptions->BCPLComment=1;<br>
> languageOptions->C99=1;<br>
> DiagnosticOptions* diagnosticOptions=new DiagnosticOptions();<br>
> llvm::raw_os_ostream os(cout);<br>
> TextDiagnosticPrinter* diagnosticClient=new<br>
> TextDiagnosticPrinter(os,*diagnosticOptions,true);<br>
> Diagnostic* diagnostic = new Diagnostic(diagnosticClient);<br>
> SourceManager* sourceManager= new SourceManager(*diagnostic);<br>
> HeaderSearch* headerSearch = new HeaderSearch(*fileManager);<br>
> TargetOptions targetOptions;<br>
> targetOptions.Triple=LLVM_HOSTTRIPLE;<br>
> TargetInfo *targetInfo = TargetInfo::CreateTargetInfo (*diagnostic,<br>
> targetOptions);<br>
> Preprocessor* preprocessor = new Preprocessor(*diagnostic,<br>
> *languageOptions, *targetInfo, *sourceManager, *headerSearch);<br>
> const FileEntry* fileEntry=fileManager->getFile("test.c");<br>
> sourceManager->createMainFileID(fileEntry);<br>
> preprocessor->EnterMainSourceFile();<br>
> Token token;<br>
> do<br>
> {<br>
> preprocessor->Lex(token);<br>
> if(diagnostic->hasErrorOccurred())<br>
> {<br>
> break;<br>
> }<br>
> preprocessor->DumpToken(token);<br>
> std::cerr << std::endl;<br>
> }<br>
> while(token.isNot(tok::eof));<br>
> diagnosticClient->EndSourceFile();<br>
> return 0;<br>
> }<br>
><br>
> This works well with input files with not errors or no warnings.<br>
><br>
> Then I give as input this very minimalistic file (test.c):<br>
><br>
> int main() {<br>
> /* Nested /* comments */<br>
> }<br>
><br>
> The parsing of this code should produce a warning for the presence of the<br>
> nested comment, but instead I get this output:<br>
><br>
> int 'int'<br>
> identifier 'main'<br>
> l_paren '('<br>
> r_paren ')'<br>
> l_brace '{'<br>
> clangExample: TextDiagnosticPrinter.cpp:298: void<br>
> clang::TextDiagnosticPrinter::EmitCaretDiagnostic(clang::SourceLocation,<br>
> clang::CharSourceRange*, unsigned int, const clang::SourceManager&, const<br>
> clang::FixItHint*, unsigned int, unsigned int, unsigned int, unsigned int,<br>
> unsigned int): Assertion `LangOpts && "Unexpected diagnostic outside source<br>
> file processing"' failed.<br>
><br>
> The problem is that the preprocessor tries to output the diagnostic message<br>
> outside the right context. I also tried to call the<br>
> diagnosticClient->BeginSourceFile function before the parsing of the file<br>
> but no warning is displayed either, the parsing is stopped with no messages<br>
> at all.<br>
><br>
> I also tried the CompilerInstance/CompilerInvocation approach with the same<br>
> result.<br>
><br>
> How can I fix this issue? Where am I wrong ?<br>
><br>
> Any help is greatly appreciated.<br>
<br>
</div></div>Your initialization code is probably wrong somehow, but these sorts of<br>
errors can be difficult to precisely locate. Please use use the<br>
frontend infrastructure like the example in<br>
examples/clang-interpreter/ does, and inherit from<br>
PreprocessorFrontendAction to build your own action similar to<br>
DumpRawTokensAction in lib/Frontend/FrontendActions.cpp.<br>
<font color="#888888"><br>
-Eli<br>
</font></blockquote></div><br>