c++parsingclanglibclang

Clang Tooling Preprocess Source Files


I am using the Clang::Tooling library to parse some header files. I can't seem to parse properly due to clang not pre-processing header files and other pre-processor stuff. How can I tell Clang::Tooling to pre-process files before parsing. Cheers. This is my current code for invoking my tool.

/*static*/ SAST SAST::Parse( CFile& HeaderFile, const TArray<CString>& CommandLineArgs )
{
    //Our Custom Formated Ast Data Struct
    SAST AST;

    //Parse Command-Line Args.
    clang::tooling::CommandLineArguments CommandArgs;
    for (auto& Item : CommandLineArgs)
        CommandArgs.push_back(Item.GetRaw());

    //Traverse And Collect AST
    auto SourceText = HeaderFile.GetText();
    auto SourceFileName = HeaderFile.GetFullName();
    clang::tooling::runToolOnCodeWithArgs(new CollectASTAction(&AST), SourceText.GetRawConst(), CommandArgs);

    return AST;
}

Solution

  • Apparently it actually is trying to pre-process the file. I couldn't see any errors reported due to no console window open. However, when I inspected the llvm::raw_fd_ostream class's write() method with a break-point, I could see that errors were being written such as "FileXXX.h could not be found". So it simply couldn't find the #include files. That's why pre-processing wasn't performed/completed. Thanks everyone. This solves my day long journey.

    EDIT:

    For those that are wondering how to specify include files:

    //Parse Command-Line Args.
    clang::tooling::CommandLineArguments CommandArgs;
    for(auto& Item : CommandLineArgs)
       CommandArgs.push_back(Item.GetRaw());
    

    In this context CommandLineArgs includes header files like this

    -IFullPathToHeaderFile

    -IFullPathToAnotherHeader

    as well as any other flag supported by clang.