Relations between Source files, Source locations and AST?

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,
I tried both something like:

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

and changing the source location to:


but in both the cases I obtain the following error:

<invalid loc>Assertion failed: (Loc.isValid() && "Invalid location"),
function getLocationOffsetAndFileID, file
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!