Comments (16)
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.
Hmm. What about this flag?
from dstep.
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.
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.
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.
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.
I'm using libclang, the C API.
from dstep.
@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.
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.
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.
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.
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.
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.
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.
@ciechowoj should I close it now or do you want me to wait for the unit tests?
from dstep.
Actually it was question for you, but I'll add unit tests first : )
from dstep.
Related Issues (20)
- Pointer arithmetic and parentheses elision
- Dstep + bindbc HOT 1
- `--normalize-modules` insert underscore in the module decl but not in the filename
- typedef function prototypes don't retain arguments HOT 5
- dub run failure - ld cannot find @linker_flags.txt HOT 4
- doc error:
- Missing import HOT 2
- Unhandled type kind objCTypeParam HOT 2
- const T arr[] gets converted to imcompatible const(T) *arr instead of a warning HOT 2
- Improper output when importing core.stdc.limits HOT 1
- Rename enum members can create invalid enum names HOT 1
- API macro erased but should be extern(System) HOT 2
- /usr/include/cuda_runtime_api.h ==> d: Error: undefined identifier `cudaError_t` HOT 6
- Assertion Failure when defining a cast from int to function pointer
- Unhandled type kind objCObject when trying to bind Foundation.framework on macOS
- enh? add enum alias (as dpp do) HOT 1
- unknown type name 'size_t' HOT 1
- `--alias-enum-members=true` shouldn't affect global anonymous enums
- Unnecessary leading dot in alias
- Add support for adding public imports
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dstep.