Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8600167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:33:25+00:00 2026-06-12T01:33:25+00:00

Consider the following simple header, demo.h: #define PERSIST struct Serialised { int someTransientValue ;

  • 0

Consider the following simple header, demo.h:

#define PERSIST
struct Serialised
{
    int someTransientValue ;
    PERSIST int aNumberToPersist ;
};

I use the following code and Clang’s python API to iterate over the header:

import sys, clang.cindex
def callexpr_visitor(node, parent, userdata):
    if node.location.file: print node.location.file, node.displayname, node.kind
    return 2
tu = clang.cindex.Index.create().parse(sys.argv[1], args=['-x', 'c++'])
clang.cindex.Cursor_visit(tu.cursor, clang.cindex.Cursor_visit_callback(callexpr_visitor), None)

This prints out the elements of Clang’s AST, producing the following output:

demo.h Serialised CursorKind.STRUCT_DECL
demo.h someTransientValue CursorKind.FIELD_DECL
demo.h aNumberToPersist CursorKind.FIELD_DECL

Does anyone know how I can extract the preprocessor declaration associated with the member variable called ‘aNumberToPersist’?, is there a better way to ‘tag’ variables in a manner that manifests clearly in the parse tree?

Xubuntu 12.04, clang version 3.1 (tags/RELEASE_31/final), Target: x86_64-unknown-linux-gnu
Thread model: posix.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:33:26+00:00Added an answer on June 12, 2026 at 1:33 am

    I would probably say: not this way.

    Macros are not represented in the AST per se. Types, Attributes, etc… all those elements that have semantic values are represented (and comments), and optionally you can query whether some of them were expanded from a macro and get back that original macro spelling; however macros in themselves do not appear in the AST, at all.

    If you could, it might be more interesting to extend Clang with a new attribute, especially in C++11: [[gearoid::persist]]. C++11 requires that compilers ignore attributes they know not about, so by “namespacing” your own attributes you pretty much guarantee that only you will care about their meaning.

    I know not, unfortunately, whether you will need to teach Clang about your attributes so they are represented in the AST (Michael Han is working on always memorizing them). In any case, you may get more useful answers on the Clang DEV mailing list (unfortunately, there is no Clang Users mailing list).

    EDIT: Just landed today! Clang will now retain all attributes (even those it does not understand) in its AST.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following simple code. struct test { test(int n):numelements(n){ arr = new int[numelements]
Please consider the following simple use case: public class Foo { public virtual int
Let's consider the following simple expressions in Java. char c='A'; int i=c+1; System.out.println(i =
Consider the following simple piece of code: %hash = ('a'=>1,'b'=>2); print $hash{'b'}; print \n,(\%hash)->{'b'};
Consider the following simple python code >>> L = range(3) >>> L [0, 1,
Consider the following simple code to create a typesafe equals. This first section allows
Consider the following simple code: List <?> list4[] = { Arrays.asList(1,2), Arrays.asList(1,2)}; I do
Consider the following simple code pattern: foreach(Item item in itemList) { if(item.Foo) { DoStuff(item);
Consider following simple example: #!perl -w use strict; sub max { my ($a, $b)
Consider the following simple code import java.util.*; public class MainTest<T extends Object1<?,?>> { List<T>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.