authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-28 23:18:18-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-28 23:18:39-07:00
log923e78785d8505d16026f1c2b78079b79adac8fa
tree0d06c02dc1f4689e22652a42f7bf8e34a8857b77
parent650fdded29da7d1e741d17c8a15c93c511266547

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.

1 files changed, 32 insertions(+), 0 deletions(-)

src/parseh.cpp+32
...@@ -713,6 +713,30 @@ static void render_aliases(Context *c) {...@@ -713,6 +713,30 @@ static void render_aliases(Context *c) {
713 }713 }
714}714}
715715
716static void process_preprocessor_entities(Context *c, ASTUnit &unit) {
717 for (PreprocessedEntity *entity : unit.getLocalPreprocessingEntities()) {
718 switch (entity->getKind()) {
719 case PreprocessedEntity::InvalidKind:
720 case PreprocessedEntity::InclusionDirectiveKind:
721 case PreprocessedEntity::MacroExpansionKind:
722 continue;
723 case PreprocessedEntity::MacroDefinitionKind:
724 {
725 MacroDefinitionRecord *macro = static_cast<MacroDefinitionRecord *>(entity);
726 const char *name = macro->getName()->getNameStart();
727 fprintf(stderr, "definition macro: %s\n", name);
728 SourceRange range = macro->getSourceRange();
729 SourceLocation begin_loc = range.getBegin();
730 SourceLocation end_loc = range.getEnd();
731
732 const char *start_c = c->source_manager->getCharacterData(begin_loc);
733 const char *end_c = c->source_manager->getCharacterData(end_loc);
734 fprintf(stderr, "source: '%.*s'\n", (int)(end_c - start_c), start_c);
735 }
736 }
737 }
738}
739
716int parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *source,740int parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *source,
717 const char **args, int args_len, const char *libc_include_path, bool warnings_on)741 const char **args, int args_len, const char *libc_include_path, bool warnings_on)
718{742{
...@@ -774,6 +798,12 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors,...@@ -774,6 +798,12 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors,
774798
775 // we don't need spell checking and it slows things down799 // we don't need spell checking and it slows things down
776 clang_argv->append("-fno-spell-checking");800 clang_argv->append("-fno-spell-checking");
801
802 // this gives us access to preprocessing entities, presumably at
803 // the cost of performance
804 clang_argv->append("-Xclang");
805 clang_argv->append("-detailed-preprocessing-record");
806
777 // to make the end argument work807 // to make the end argument work
778 clang_argv->append(nullptr);808 clang_argv->append(nullptr);
779809
...@@ -844,6 +874,8 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors,...@@ -844,6 +874,8 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors,
844 c->root = create_node(c, NodeTypeRoot);874 c->root = create_node(c, NodeTypeRoot);
845 ast_unit->visitLocalTopLevelDecls(c, decl_visitor);875 ast_unit->visitLocalTopLevelDecls(c, decl_visitor);
846876
877 process_preprocessor_entities(c, *ast_unit);
878
847 render_aliases(c);879 render_aliases(c);
848880
849 normalize_parent_ptrs(c->root);881 normalize_parent_ptrs(c->root);