Clang: Pass by const-ref C++0x


I’ve been working with Clang for a couple of days and I can’t help feeling like I am missing something. When I parse out the following:

void HandleBlah(std::string const& in, std::string& out)


// do something


It doesn’t seem to recognize the qualifier const. However if I omit the reference it seems as though it works. It doesn’t seem as if there is any indication either in ParmVarDecl or Qualifiers or QualType that a developer can deduce if the parameter is a pointer or reference type. How can I detect whether or not a parameter is a reference? I suspect it is something to do in how I have set up my project. I am using a SemaConsumer being passed into the Process function and I only care about top level function declarations. Thank you in advance!


To summarize the setup:


  • To use:

    1. Create compiler instance
    1. Create a stream with NewOutputStream
    1. Call Setup with obvious parameters.
    1. Create your consumer object.
    1. Call Process with your consumer.

The answer lies in the difference between these two types:

typedef char const *constant_string;
typedef char * const fixed_string;

The former is a mutable pointer to immutable characters, the latter is an immutable pointer to mutable characters.

QualType is actually just a wrapper around Type (and its subclasses). Pretty much anything you can const_cast away lives on QualType (the Qualifiers), but for everything else you'll want to pull out the underlying Type and work with that. Conveniently, you can use * and -> on QualType to get at the Type like a smart pointer.

So the way those typedefs are built in Clang looks like this:

TypedefType(constant_string) -> QualType(no qualifiers) -> PointerType -> QualType(const) -> BuiltinType(char)
TypedefType(fixed_string) -> QualType(const) -> PointerType -> Qualtype(no qualifiers) -> BuiltinType(char)

(I'm doing these from memory, so the details are probably a bit off.)

And your example of 'std::string const &' looks like this:

QualType(no qualifiers) -> ReferenceType -> QualType(const) -> RecordType(std::string)

Hope that helps,