How to add custom include dir to the header search path?

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

How to add custom include dir to the header search path?

huc1985
I'm writing a tools (do some static check) using clang lib . This is my code:

void main()
{
    DiagnosticOptions diagnosticOptions;
    TextDiagnosticPrinter *printer = new TextDiagnosticPrinter(llvm::nulls(),
        diagnosticOptions);
    llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagIDs;
    DiagnosticsEngine diagnostics(diagIDs, printer);
    LangOptions langOpts;

    TargetOptions targetOpts;
    targetOpts.Triple = llvm::sys::getDefaultTargetTriple();
    TargetInfo *pti = TargetInfo::CreateTargetInfo(diagnostics, targetOpts);
    FileSystemOptions fsopts;
    FileManager fileManager(fsopts);
    SourceManager sourceManager(diagnostics, fileManager);

    HeaderSearch headerSearch(fileManager, diagnostics, langOpts, pti);

    CompilerInstance *theCompInst = new CompilerInstance();
    theCompInst->setDiagnostics(&diagnostics);
    Preprocessor *pp = new Preprocessor(diagnostics, langOpts, pti,sourceManager, headerSearch, *theCompInst);

    const FileEntry *pFile = fileManager.getFile(file_name);
    sourceManager.createMainFileID(pFile);
    theCompInst->getDiagnosticClient().BeginSourceFile(theCompInst->getLangOpts(),pp);
    theCompInst->setPreprocessor(pp);
    theCompInst->setSourceManager(&sourceManager);
    theCompInst->setTarget(pti);
    theCompInst->setDiagnostics(&diagnostics);
    SourceManager &sourceMgr = theCompInst->getSourceManager();
    theCompInst->createASTContext();
    Rewriter rewriter;
    rewriter.setSourceMgr(sourceMgr,langOpts);


    MyASTConsumer consumer(rewriter);
    ParseAST(theCompInst->getPreprocessor(), &consumer, theCompInst->getASTContext());
    theCompInst->getDiagnosticClient().EndSourceFile();
}

I want to check some C/C++ code using this tools. But *.h file and *.c file is not in the same filefold.

Example:
  test.c context:
    #include "test.h"
     .........

test.c in D:\test\test.c
test.h in D:\test\include\test.h

So I need to add "D:\test\include" to the header search path. How to do that in my code?
Thanks all!
Reply | Threaded
Open this post in threaded view
|

Re: How to add custom include dir to the header search path?

Robert Ankeney
Something like:
HeaderSearchOptions &headerSearchOptions = theCompInst->getHeaderSearchOpts();
headerSearchOptions.AddPath("D:/test/include", clang::frontend::Angled, false, false, false);

should do it.



I'm writing a tools (do some static check) using clang lib . This is my code:

void main()
{
    DiagnosticOptions diagnosticOptions;
    TextDiagnosticPrinter *printer = new
TextDiagnosticPrinter(llvm::nulls(),
        diagnosticOptions);
    llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagIDs;
    DiagnosticsEngine diagnostics(diagIDs, printer);
    LangOptions langOpts;

    TargetOptions targetOpts;
    targetOpts.Triple = llvm::sys::getDefaultTargetTriple();
    TargetInfo *pti = TargetInfo::CreateTargetInfo(diagnostics, targetOpts);
    FileSystemOptions fsopts;
    FileManager fileManager(fsopts);
    SourceManager sourceManager(diagnostics, fileManager);

    HeaderSearch headerSearch(fileManager, diagnostics, langOpts, pti);

    CompilerInstance *theCompInst = new CompilerInstance();
    theCompInst->setDiagnostics(&diagnostics);
    Preprocessor *pp = new Preprocessor(diagnostics, langOpts,
pti,sourceManager, headerSearch, *theCompInst);

    const FileEntry *pFile = fileManager.getFile(file_name);
    sourceManager.createMainFileID(pFile);

theCompInst->getDiagnosticClient().BeginSourceFile(theCompInst->getLangOpts(),pp);
    theCompInst->setPreprocessor(pp);
    theCompInst->setSourceManager(&sourceManager);
    theCompInst->setTarget(pti);
    theCompInst->setDiagnostics(&diagnostics);
    SourceManager &sourceMgr = theCompInst->getSourceManager();
    theCompInst->createASTContext();
    Rewriter rewriter;
    rewriter.setSourceMgr(sourceMgr,langOpts);


    MyASTConsumer consumer(rewriter);
    ParseAST(theCompInst->getPreprocessor(), &consumer,
theCompInst->getASTContext());
    theCompInst->getDiagnosticClient().EndSourceFile();
}

I want to check some C/C++ code using this tools. But *.h file and *.c file
is not in the same filefold.

Example:
  test.c context:
    #include "test.h"
     .........

test.c in D:\test\test.c
test.h in D:\test\include\test.h

So I need to add "D:\test\include" to the header search path. How to do that
in my code?
Thanks all!



--
View this message in context: http://clang-developers.42468.n3.nabble.com/How-to-add-custom-include-dir-to-the-header-search-path-tp4031389.html
Sent from the Clang Developers mailing list archive at Nabble.com.


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

Re: How to add custom include dir to the header search path?

Welson Sun
No, this doesn't work, at least with 3.1.

Search headerSearch or headerSearchOptions in this mailing list, there are a lot of similar threads.


On Tue, Apr 9, 2013 at 10:43 AM, Robert Ankeney <[hidden email]> wrote:
Something like:
HeaderSearchOptions &headerSearchOptions = theCompInst->getHeaderSearchOpts();
headerSearchOptions.AddPath("D:/test/include", clang::frontend::Angled, false, false, false);

should do it.




I'm writing a tools (do some static check) using clang lib . This is my code:

void main()
{
    DiagnosticOptions diagnosticOptions;
    TextDiagnosticPrinter *printer = new
TextDiagnosticPrinter(llvm::nulls(),
        diagnosticOptions);
    llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagIDs;
    DiagnosticsEngine diagnostics(diagIDs, printer);
    LangOptions langOpts;

    TargetOptions targetOpts;
    targetOpts.Triple = llvm::sys::getDefaultTargetTriple();
    TargetInfo *pti = TargetInfo::CreateTargetInfo(diagnostics, targetOpts);
    FileSystemOptions fsopts;
    FileManager fileManager(fsopts);
    SourceManager sourceManager(diagnostics, fileManager);

    HeaderSearch headerSearch(fileManager, diagnostics, langOpts, pti);

    CompilerInstance *theCompInst = new CompilerInstance();
    theCompInst->setDiagnostics(&diagnostics);
    Preprocessor *pp = new Preprocessor(diagnostics, langOpts,
pti,sourceManager, headerSearch, *theCompInst);

    const FileEntry *pFile = fileManager.getFile(file_name);
    sourceManager.createMainFileID(pFile);

theCompInst->getDiagnosticClient().BeginSourceFile(theCompInst->getLangOpts(),pp);
    theCompInst->setPreprocessor(pp);
    theCompInst->setSourceManager(&sourceManager);
    theCompInst->setTarget(pti);
    theCompInst->setDiagnostics(&diagnostics);
    SourceManager &sourceMgr = theCompInst->getSourceManager();
    theCompInst->createASTContext();
    Rewriter rewriter;
    rewriter.setSourceMgr(sourceMgr,langOpts);


    MyASTConsumer consumer(rewriter);
    ParseAST(theCompInst->getPreprocessor(), &consumer,
theCompInst->getASTContext());
    theCompInst->getDiagnosticClient().EndSourceFile();
}

I want to check some C/C++ code using this tools. But *.h file and *.c file
is not in the same filefold.

Example:
  test.c context:
    #include "test.h"
     .........

test.c in D:\test\test.c
test.h in D:\test\include\test.h

So I need to add "D:\test\include" to the header search path. How to do that
in my code?
Thanks all!



--
View this message in context: http://clang-developers.42468.n3.nabble.com/How-to-add-custom-include-dir-to-the-header-search-path-tp4031389.html
Sent from the Clang Developers mailing list archive at Nabble.com.


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




--
Welson

Phone: (408) 418-8385


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

Re: How to add custom include dir to the header search path?

huc1985
Thanks a lot!
I write like this, it's work!
HeaderSearchOptions headerSearchOptions;
HeaderSearchOptions.AddPath("D:\\test\\include",frontend::Quoted,false,false,false);
ApplyHeaderSearchOptions(headerSearch, headerSearchOptions, languageOptions, pTargetInfo->getTriple());