[cfe-dev] Relations between Source files, Source locations and AST?

GabrieleCocco cocco at di.unipi.it
Sun Oct 28 17:45:51 PDT 2012


Hi,
I'm performing some rewriting of C source code. At a certain point, I would
like to add an include directive just before the first declaration inside
the main file of the translation unit (or after the last include directive,
eventually).
I tried both something like:

        // Add include directive for global structures before the first
declaration
        DeclContext::decl_iterator iterator =
Context.getTranslationUnitDecl()->decls_begin();
        Decl* first_decl = *iterator;
        rewriter.InsertText(first_decl->getLocation(), "#include \"" +
global_header_file_name + "\"");

and changing the source location to:

       
Context.getSourceManager().getIncludeLoc(Context.getSourceManager().getMainFileID());

but in both the cases I obtain the following error:

<invalid loc>Assertion failed: (Loc.isValid() && "Invalid location"),
function getLocationOffsetAndFileID, file
/Users/Gabriele/Documents/LLVM/llvm/tools/clang/lib/Rewrite/Core/Rewriter.cpp,
line 212.

This is probably due to the fact that I didn't understood completely the
relationship between SourceLocations, source files and AST.
Could you point me to any documentation on this subject? Or alternatively,
can you give me some hints to solve the problem?

Thank you very much!



--
View this message in context: http://clang-developers.42468.n3.nabble.com/Relations-between-Source-files-Source-locations-and-AST-tp4027801.html
Sent from the Clang Developers mailing list archive at Nabble.com.



More information about the cfe-dev mailing list