ASTContext: how to get an object or pointer of ASTContext

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

ASTContext: how to get an object or pointer of ASTContext

Jiandong Wang
Hi all:
    now I am trying to run my own ASTConsumer against AST, but not quite sure how to do it, especially how to get an object or pointer of ASTContext. BTW, ASTContext is required by ParseAST.  
here is my code snippet:
_______________________________________

        MyDiagnosticClient DiagClient(llvm::errs());
Diagnostic Diags(&DiagClient);
LangOptions Opts;
        TargetOptions TargetOpts;
        TargetInfo* Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
SourceManager SM;
FileManager FM;
HeaderSearch Headers(FM);

llvm::StringRef filename("input.c");
const FileEntry* File = FM.getFile(filename);
if (!File) {
 llvm::errs() << "Failed to open \'" << filename << "\'\n" ;
 return 1;
}

SM.createMainFileID(File, SourceLocation());
Preprocessor PP(Diags, Opts, *Target, SM, Headers);
ASTConsumer* AstConsumer = new MyASTConsumer;
ParseAST(PP, AstConsumer, *here need the third parameter ASTContext*);     //<------------------------- see here
delete Target;
______________________________________

any ideas? Thanks in advance.

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: ASTContext: how to get an object or pointer of ASTContext

Douglas Gregor

On May 18, 2010, at 6:09 AM, Jiandong Wang wrote:

Hi all:
    now I am trying to run my own ASTConsumer against AST, but not quite sure how to do it, especially how to get an object or pointer of ASTContext. BTW, ASTContext is required by ParseAST.  
here is my code snippet:
_______________________________________

        MyDiagnosticClient DiagClient(llvm::errs());
Diagnostic Diags(&DiagClient);
LangOptions Opts;
        TargetOptions TargetOpts;
        TargetInfo* Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
SourceManager SM;
FileManager FM;
HeaderSearch Headers(FM);

llvm::StringRef filename("input.c");
const FileEntry* File = FM.getFile(filename);
if (!File) {
 llvm::errs() << "Failed to open \'" << filename << "\'\n" ;
 return 1;
}

SM.createMainFileID(File, SourceLocation());
Preprocessor PP(Diags, Opts, *Target, SM, Headers);
ASTConsumer* AstConsumer = new MyASTConsumer;
ParseAST(PP, AstConsumer, *here need the third parameter ASTContext*);     //<------------------------- see here
delete Target;
______________________________________

any ideas? Thanks in advance.

You can just create an ASTContext on the stack, passing in the various other pieces (lang options, source manager, target info, etc.) when you construct it.

Have you considered using CompilerInvocation/CompilerInstance rather than building all of the pieces together?

- Doug

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: ASTContext: how to get an object or pointer of ASTContext

Jiandong Wang


On Tue, May 18, 2010 at 10:11 PM, Douglas Gregor <[hidden email]> wrote:

On May 18, 2010, at 6:09 AM, Jiandong Wang wrote:

Hi all:
    now I am trying to run my own ASTConsumer against AST, but not quite sure how to do it, especially how to get an object or pointer of ASTContext. BTW, ASTContext is required by ParseAST.  
here is my code snippet:

You can just create an ASTContext on the stack, passing in the various other pieces (lang options, source manager, target info, etc.) when you construct it.
 
 Thank you. I finally compiled it through.my command:
       g++ `llvm-config --cxxflags --ldflags` -fno-rtti -c Myfile.cpp -o Myfile.o
but when I generated the executable, unexpected errors occured. my command:
       g++ `llvm-config --cxxflasg --ldflags --libs core` -o Myfile Myfile.o 

errors like this:
undefined reference to 'llvm::errs()'
undefined reference to 'clang::Diagnostic::Diagnostic(clang::DiagnosticClient*)'
undefined reference to 'clang::FileManager::FileManager()'

missing something? 

Have you considered using CompilerInvocation/CompilerInstance rather than building all of the pieces together?

  Yes, I knew. I will try it later. -:)


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: ASTContext: how to get an object or pointer of ASTContext

Douglas Gregor

On May 18, 2010, at 6:19 PM, Jiandong Wang wrote:



On Tue, May 18, 2010 at 10:11 PM, Douglas Gregor <[hidden email]> wrote:

On May 18, 2010, at 6:09 AM, Jiandong Wang wrote:

Hi all:
    now I am trying to run my own ASTConsumer against AST, but not quite sure how to do it, especially how to get an object or pointer of ASTContext. BTW, ASTContext is required by ParseAST.  
here is my code snippet:

You can just create an ASTContext on the stack, passing in the various other pieces (lang options, source manager, target info, etc.) when you construct it.
 
 Thank you. I finally compiled it through.my command:
       g++ `llvm-config --cxxflags --ldflags` -fno-rtti -c Myfile.cpp -o Myfile.o
but when I generated the executable, unexpected errors occured. my command:
       g++ `llvm-config --cxxflasg --ldflags --libs core` -o Myfile Myfile.o 

errors like this:
undefined reference to 'llvm::errs()'
undefined reference to 'clang::Diagnostic::Diagnostic(clang::DiagnosticClient*)'
undefined reference to 'clang::FileManager::FileManager()'

missing something? 

You need to link against the Clang libraries. -lClangBasic -lClang -lClangAST and so on.

- Doug


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: ASTContext: how to get an object or pointer of ASTContext

Jiandong Wang
the same errors are still there. But when I use -lCIndex instead of -lclangBasic(etc.),  it links successfully. Another error appears when it comes to running the executable(by ./Myfile). 
error like this:
error while loading shared libraries: libCIndex.so: cannot open shared object file: No such file or directory


On Wed, May 19, 2010 at 9:22 AM, Douglas Gregor <[hidden email]> wrote:

On May 18, 2010, at 6:19 PM, Jiandong Wang wrote:



On Tue, May 18, 2010 at 10:11 PM, Douglas Gregor <[hidden email]> wrote:

On May 18, 2010, at 6:09 AM, Jiandong Wang wrote:

Hi all:
    now I am trying to run my own ASTConsumer against AST, but not quite sure how to do it, especially how to get an object or pointer of ASTContext. BTW, ASTContext is required by ParseAST.  
here is my code snippet:

You can just create an ASTContext on the stack, passing in the various other pieces (lang options, source manager, target info, etc.) when you construct it.
 
 Thank you. I finally compiled it through.my command:
       g++ `llvm-config --cxxflags --ldflags` -fno-rtti -c Myfile.cpp -o Myfile.o
but when I generated the executable, unexpected errors occured. my command:
       g++ `llvm-config --cxxflasg --ldflags --libs core` -o Myfile Myfile.o 

errors like this:
undefined reference to 'llvm::errs()'
undefined reference to 'clang::Diagnostic::Diagnostic(clang::DiagnosticClient*)'
undefined reference to 'clang::FileManager::FileManager()'

missing something? 

You need to link against the Clang libraries. -lClangBasic -lClang -lClangAST and so on.

- Doug



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev