Parse AST several times

Hi,
   I am creating and parsing an AST. During this parsing, I am making some
changes(using rewriter) to the file. I want to now update the AST and parse
the AST conforming to the edited file. Unfortunately, the
sourcemanager.createMianFileID(FileEntry) gives an error saying
"MainFileID.isInvalid() && MainFileID already exists". How can I parse the
AST several time, while updating the AST after each parsing.

Regards,
Adil
LUMS SSE

How about reparsing with a new SourceManager, Preprocessor etc.
For example, use a new ASTUnit or CompilerInstance, depending on how you do the parsing.

Hi,

Is it possible that once I have done the parsing, instead of a writing to a file, i go on to compile the contents of the buffer in my rewriter?

Regards,

Adil

Hi,

Is it possible that once I have done the parsing, instead of a writing to a file, i go on to compile the contents of the buffer in my rewriter?

Yes, this is achieved via SourceManager::overrideFileContents().
Depending on how you do the parsing, you may just need to add the remapping at PreprocessorOptions::RemappedFileBuffers.

Hi,
   I mean once I have overridden the contents or have the new buffer
containing updated contents, how do i actually compile it? because if i
leave it at that, the program closes without doing anything. How do i
actually go forward and generate an executable of those contents.

Regards,
Adil

Hi,
  I mean once I have overridden the contents or have the new buffer
containing updated contents, how do i actually compile it? because if i
leave it at that, the program closes without doing anything. How do i
actually go forward and generate an executable of those contents.

A somewhat simple way would be to write the buffer to a temporary file and then just invoke the clang executable to do the compiling with the addition of passing "-remap-file" as argument.
See "test/Misc/remap-file.c" for an example of such invocation.

Hi,

While parsing the AST, I am making additions to the code. I want to find out the location of the file where global variables can be added. e.g.

#include <stdio.h>

– Want to find this location to insert global variables here

int main()
{
}

How can I find that location. Also how does clang represent this statement “using namespace std;”?

Regards,
Adil

Hi,
   While parsing the AST, I am making additions to the code. I want to find out the location of the file where global variables can be added. e.g.

#include <stdio.h>

-- Want to find this location to insert global variables here

int main()
{
}
  
   How can I find that location.

See Lexer::ComputePreamble()

Also how does clang represent this statement "using namespace std;"?

Try using the "-ast-dump" compiler argument to explore how the AST is formed from a source file.