From 923e78785d8505d16026f1c2b78079b79adac8fa Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 28 Jan 2016 23:18:18 -0700 Subject: [PATCH] attempt to get preprocessor entities from libclang I'm pretty sure it's impossible, the API does not expose the macro expansion value and the source location points to the macro's name, not its value. --- src/parseh.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/parseh.cpp b/src/parseh.cpp index 5a829edc4b..e87daa5fc6 100644 --- a/src/parseh.cpp +++ b/src/parseh.cpp @@ -713,6 +713,30 @@ static void render_aliases(Context *c) { } } +static void process_preprocessor_entities(Context *c, ASTUnit &unit) { + for (PreprocessedEntity *entity : unit.getLocalPreprocessingEntities()) { + switch (entity->getKind()) { + case PreprocessedEntity::InvalidKind: + case PreprocessedEntity::InclusionDirectiveKind: + case PreprocessedEntity::MacroExpansionKind: + continue; + case PreprocessedEntity::MacroDefinitionKind: + { + MacroDefinitionRecord *macro = static_cast(entity); + const char *name = macro->getName()->getNameStart(); + fprintf(stderr, "definition macro: %s\n", name); + SourceRange range = macro->getSourceRange(); + SourceLocation begin_loc = range.getBegin(); + SourceLocation end_loc = range.getEnd(); + + const char *start_c = c->source_manager->getCharacterData(begin_loc); + const char *end_c = c->source_manager->getCharacterData(end_loc); + fprintf(stderr, "source: '%.*s'\n", (int)(end_c - start_c), start_c); + } + } + } +} + int parse_h_buf(ImportTableEntry *import, ZigList *errors, Buf *source, const char **args, int args_len, const char *libc_include_path, bool warnings_on) { @@ -774,6 +798,12 @@ int parse_h_file(ImportTableEntry *import, ZigList *errors, // we don't need spell checking and it slows things down clang_argv->append("-fno-spell-checking"); + + // this gives us access to preprocessing entities, presumably at + // the cost of performance + clang_argv->append("-Xclang"); + clang_argv->append("-detailed-preprocessing-record"); + // to make the end argument work clang_argv->append(nullptr); @@ -844,6 +874,8 @@ int parse_h_file(ImportTableEntry *import, ZigList *errors, c->root = create_node(c, NodeTypeRoot); ast_unit->visitLocalTopLevelDecls(c, decl_visitor); + process_preprocessor_entities(c, *ast_unit); + render_aliases(c); normalize_parent_ptrs(c->root);