Deep const experiment - how to retrieve SourceLocation of "const"?

Dear Clang developers, I am experimenting with “poor man’s deep const” in C++.

I have the following C++ code:

#define immutable const

struct CC { immutable int myInt; };

I would like to use Clang to check if the type qualifier source code reads “const” or “immutable”.

I am able to retrieve the clang::FieldDecl and it’s QualType.

Is it possible, given my fieldDecl, to retrieve it’s DeclSpec?

If I had a DeclSpec, I would then call DeclSpec::getConstSpecLoc() and retrieve the source code at that location.

Here are two of the approaches I already tried:

  1. called fieldDecl->getSourceRange(); but that returns range only for “int myInt”

  2. retrieved my field’s TypeLoc and cast it to a QualifiedTypeLoc.

But, as the header file warns, the QualifiedTypeLoc does not have valid SourceRange data.

Any ideas?

Thanks for your advice,



Just noting that I have something quite similar hacked into Clang + LLVM 2.9

Regarding you example, perhaps you could use an attribute in the define to allow distinguishing immutable from const?


That’s correct. Clang does not retain the source location of every qualifier.

George R.'s suggesting of adding your own ‘immutable’ attribute is a good one.

  • Doug