Giter Site home page Giter Site logo

Comments (16)

jacob-carlborg avatar jacob-carlborg commented on July 2, 2024

Yes, there are plans to handle this. Unfortunately libclang doesn't provide an API to do this yet. Although, the C++ libraries provides API's to do this. I, or someone else, just need to add an API to libclang for this. Contributions are welcome 😉.

from dstep.

aldanor avatar aldanor commented on July 2, 2024

Hmm. What about this flag?

http://clang.llvm.org/doxygen/group__CINDEX__TRANSLATION__UNIT.html#ggab1e4965c1ebe8e41d71e90203a723fe9a9e1dd69c4a2ff7954dca8df41a63c0e6

from dstep.

jacob-carlborg avatar jacob-carlborg commented on July 2, 2024

There's still no API available to get access to any of this information, as far as I know. I've asked in the Clang mailing list.

from dstep.

aldanor avatar aldanor commented on July 2, 2024

I've skimmed through the clang repo, there is PreprocessingRecord class which seems to track all of the preprocessing, and the only place in code where seems to be invoked is in CompilerInstance::createPreprocessor(), only when the PreprocessorOptions.DetailedRecord flag is set to true (i.e. the CXTranslationUnit_DetailedPreprocessingRecord flag is passed). Why wouldn't that work, am I missing something? Hm...

from dstep.

jacob-carlborg avatar jacob-carlborg commented on July 2, 2024

Even if I pass the CXTranslationUnit_DetailedPreprocessingRecord flag, which API should I use. Example:

#define foo 1

How to I get the value, 1?

from dstep.

aldanor avatar aldanor commented on July 2, 2024

Ok, please mind my clang, I have never used it before. So here's goes the header, then the program output, then the C++ code itself.

// target.h
#define a (1)
#define f(x, y) ((x) + (y))

#ifdef __QWE__
        #define __QWEQWE__
#endif

int qwe(int x);

int qweqwe() {
        return 1;
}

Note the MACRO entries:

int 'int'
identifier 'qwe'
l_paren '('
int 'int'
identifier 'x'
r_paren ')'
semi ';'
int 'int'
identifier 'qweqwe'
l_paren '('
r_paren ')'
l_brace '{'
return 'return'
numeric_constant '1'
semi ';'
r_brace '}'
eof ''
target.h:1:9 -> MACRO: l_paren '('  numeric_constant '1'  r_paren ')'
target.h:2:9 -> MACRO: l_paren '('  l_paren '('  identifier 'x'  r_paren ')'  plus '+'  l_paren '('  identifier 'y'  r_paren ')'  r_paren ')'

The code (I used string comparison on source location, that's kinda ugly but I don't know clang API well enough to check if the macro is defined in the target file; you might be able to do this better):

int main()
{
    CompilerInstance ci;
    DiagnosticOptions diagnosticOptions;
    ci.createDiagnostics();
    llvm::IntrusiveRefCntPtr<TargetOptions> pto( new TargetOptions());
    pto->Triple = llvm::sys::getDefaultTargetTriple();
    TargetInfo *pti = TargetInfo::CreateTargetInfo(ci.getDiagnostics(), pto.getPtr());
    ci.setTarget(pti);
    ci.createFileManager();
    ci.createSourceManager(ci.getFileManager());
    ci.createPreprocessor();
    ci.getPreprocessorOpts().DetailedRecord = true;
    const FileEntry *pFile = ci.getFileManager().getFile("target.h");
    ci.getSourceManager().createMainFileID(pFile);
    ci.getPreprocessor().EnterMainSourceFile();
    ci.getDiagnosticClient().BeginSourceFile(ci.getLangOpts(),
                                             &ci.getPreprocessor());
    Token tok;
    do {
        ci.getPreprocessor().Lex(tok);
        if (ci.getDiagnostics().hasErrorOccurred())
            break;
        ci.getPreprocessor().DumpToken(tok);
        std::cerr << std::endl;
    } while ( tok.isNot(clang::tok::eof));
    ci.getDiagnosticClient().EndSourceFile();
    for (Preprocessor::macro_iterator I = ci.getPreprocessor().macro_begin(),
            E = ci.getPreprocessor().macro_end(); I != E; ++I) {
        MacroInfo *MI = I->second->getMacroInfo();
        SourceLocation loc = MI->getDefinitionLoc();
        if (!loc.isFileID())
            continue;
        std::string strLoc = loc.printToString(ci.getSourceManager());
        if (strLoc.find("target.h") != std::string::npos) {
            std::cerr << strLoc << " -> ";
            ci.getPreprocessor().DumpMacro(*MI);
        }
    }
    return 0;
}

from dstep.

jacob-carlborg avatar jacob-carlborg commented on July 2, 2024

I'm using libclang, the C API.

from dstep.

aldanor avatar aldanor commented on July 2, 2024

@jacob-carlborg Ok, if you look at the c-index-test tool in llvm/clang/tools/c-index-test, it uses libclang exclusively and not the C++ API (link to source).

$ c-index-test -test-load-source all target.h | grep target.h

// CHECK: target.h:1:9: macro definition=a Extent=[1:9 - 1:14]
// CHECK: target.h:2:9: macro definition=f Extent=[2:9 - 2:28]
// CHECK: target.h:8:5: FunctionDecl=qwe:8:5 Extent=[8:1 - 8:15]
// CHECK: target.h:8:13: ParmDecl=x:8:13 (Definition) Extent=[8:9 - 8:14]
// CHECK: target.h:10:5: FunctionDecl=qweqwe:10:5 (Definition) Extent=[10:1 - 12:2]
// CHECK: target.h:10:14: CompoundStmt= Extent=[10:14 - 12:2]
// CHECK: target.h:11:2: ReturnStmt= Extent=[11:2 - 11:10]
// CHECK: target.h:11:9: IntegerLiteral= Extent=[11:9 - 11:10]

So it can somehow magically find the macro definitions (see the first two lines)?

from dstep.

jacob-carlborg avatar jacob-carlborg commented on July 2, 2024

Yeah, I noticed that it's possible to get information if a cursor is a macro. Although I cannot find an API to get more information than the name of the macro, the spelling.

from dstep.

aldanor avatar aldanor commented on July 2, 2024

Yea, I guess you can get the locations but not the actual text (with libclang). So, it seems the only tangible option (apart from pulling the macro text based by locations and then hacking the strings semi-manually and figuring out what each #define means, which is always an option) is having an extra tool which is based on C++ clang API (like in the example above) whose only job is to produce AST for macros? Unless you have reasons for not involving the clang C++ stack :) Idk hot ugh, maybe I'm plain wrong and there's no way to make this work..

from dstep.

jacob-carlborg avatar jacob-carlborg commented on July 2, 2024

As I said in my first reply. The correct solution is to extend the C API with the missing pieces to handle macros.

from dstep.

jacob-carlborg avatar jacob-carlborg commented on July 2, 2024

BTW, the reason for not using the C++ API's is:

  • DStep is written in D. It's much easier to interface D code with C than C++ code. I don't even know if the C++ support in D was usable when I started DStep
  • The C API is stable. It's even ABI stable. I can use any version of libclang from 3.1 to the latest, without having to recompile or relink anything

from dstep.

joakim-noah avatar joakim-noah commented on July 2, 2024

Until a C API shows up, it would be nice if you could put in some basic regexp to translate simple constant defines of the form #define foo 1 to enum foo = 1, and punt on the more complicated ones until the preprocessor APIs are worked out.

from dstep.

ciechowoj avatar ciechowoj commented on July 2, 2024

I suppose it could be closed via #67 . However I may create a file Issue20.d with unit tests with the few examples of defines mentioned here.

from dstep.

jacob-carlborg avatar jacob-carlborg commented on July 2, 2024

@ciechowoj should I close it now or do you want me to wait for the unit tests?

from dstep.

ciechowoj avatar ciechowoj commented on July 2, 2024

Actually it was question for you, but I'll add unit tests first : )

from dstep.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.