[cfe-dev] How to use libtooling to parse multiple files at once? and succesfully find stddef.h?

rayjcwu rayjcwu at gmail.com
Mon Dec 2 13:49:05 PST 2013


in short: * If I change compile_commands.json, leave only those files causing
errors while processing, my program could successfully parse all files
listed in compile_commands.json without any error.*

I compared compiling arguments for clang drivers from make (verbose mode)
and compile_commands.json but didn't find anything. If I run ClangTool to
run the FrontendAction, it will have a lot of error messages and core dump
in the end. Error messages are complaing about can't find some headers
within this project. But if I run those commands in compile_commands.json in
shell, everything works fine.

I'm using llvm/clang r194059 on ubuntu 12.04 trying to parse apache 2.4.7.
Generate compile_commands.json by https://github.com/rizsotto/Bear. Error
messages are like 

==============================================================================
Error while processing /home/jcwu/repos/httpd-2.4.7/support/htcacheclean.c.
atomic/unix/builtins.c:71:53: warning: passing 'const void *' to parameter
of type 'volatile void *' discards qualifiers
[-Wincompatible-pointer-types-discards-qualifiers]
    return (void*) __sync_val_compare_and_swap(mem, cmp, with);
                                                    ^~~
1 warning generated.
atomic/unix/builtins.c:71:53: warning: passing 'const void *' to parameter
of type 'volatile void *' discards qualifiers
[-Wincompatible-pointer-types-discards-qualifiers]
    return (void*) __sync_val_compare_and_swap(mem, cmp, with);
                                                    ^~~
1 warning generated.
repos.c:35:10: fatal error: cannot open file './mod_dav.h': No such file or
directory
#include "mod_dav.h"
         ^
1 error generated.
Error while processing /home/jcwu/repos/httpd-2.4.7/modules/dav/fs/repos.c.
repos.c:35:10: fatal error: cannot open file './mod_dav.h': No such file or
directory
#include "mod_dav.h"
         ^
1 error generated.
Error while processing /home/jcwu/repos/httpd-2.4.7/modules/dav/fs/repos.c.
lock.c:32:10: fatal error: cannot open file './mod_dav.h': No such file or
directory
#include "mod_dav.h"
         ^
1 error generated.
Error while processing /home/jcwu/repos/httpd-2.4.7/modules/dav/fs/lock.c.
lock.c:32:10: fatal error: cannot open file './mod_dav.h': No such file or
directory
#include "mod_dav.h"
         ^
1 error generated.
Error while processing /home/jcwu/repos/httpd-2.4.7/modules/dav/fs/lock.c.
clang-annotator:
/home/jcwu/repos/llvm-r194059-src/lib/Support/MemoryBuffer.cpp:57: void
llvm::MemoryBuffer::init(const char*, const char*, bool): Assertion
`(!RequiresNullTerminator || BufEnd[0] == 0) && "Buffer is not null
terminated!"' failed.
Aborted (core dumped)
==============================================================================

But the visitor didn't do anything meaningful. Even if I use an empty
visitor without override any functions, the error still exist.

The strangest thing is,* if I change compile_commands.json, leave only those
files causing errors while processing them, my program could successfully
parse all files listed in compile_commands.json without any error.*

My code is at
https://github.com/rayjcwu/clang-annotator/blob/master/parser.cpp and the
pseudo code is roughtly like the following: (I turn on exception supports
because I need to use a database library using exceptions)

// A dummy visitor doing nothing, omitted

void ExampleASTConsumer::HandleTranslationUnit(ASTContext &Context) {
  visitor->TraverseDecl(Context.getTranslationUnitDecl());
}

ASTConsumer *ExampleFrontendAction::CreateASTConsumer(CompilerInstance &CI,
StringRef file) {
  return new ExampleASTConsumer(&CI); // pass CI pointer to ASTConsumer
}

class ClangParser {
private:
    CompilationDatabase *CD;
public: 
   ClangParser(string srcPath) {
      string errorMsg = "";
      CD = CompilationDatabase::autoDetectFromDirectory (srcPath.c_str(),
errorMsg);
      if (errorMsg != "") {
         cerr << errorMsg << endl;
         throw invalid_argument(errorMsg);
      }

   void run() {
      ClangTool Tool(*CD, CD->getAllFiles());
      Tool.run(newFrontendActionFactory<ExampleFrontendAction>());
   }
}




--
View this message in context: http://clang-developers.42468.n3.nabble.com/How-to-use-libtooling-to-parse-multiple-files-at-once-and-succesfully-find-stddef-h-tp4035389p4036399.html
Sent from the Clang Developers mailing list archive at Nabble.com.



More information about the cfe-dev mailing list