[cfe-dev] Clang: Pass by const-ref C++0x

Jordan Rose jordan_rose at apple.com
Wed Jun 6 10:07:53 PDT 2012


On Jun 6, 2012, at 9:42 , "Lewis, Jason" <jason.lewis at gentex.com> wrote:

> Hello,
>  
>  
> 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!
>  
>  
> -JR

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,
Jordan

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120606/dd345697/attachment.html>


More information about the cfe-dev mailing list