[cfe-users] libclang: Spelling on typedefs lacks namespaces

Michael via cfe-users cfe-users at lists.llvm.org
Tue Sep 27 10:52:56 PDT 2016


I had a look into the clang sources. It seems this little patch would 
"fix" the problem. This was pure guess work, I don't know what 
consequences this might have.

Index: lib/AST/TypePrinter.cpp
===================================================================
--- lib/AST/TypePrinter.cpp     (revision 282393)
+++ lib/AST/TypePrinter.cpp     (working copy)
@@ -96,7 +96,7 @@

      static bool canPrefixQualifiers(const Type *T, bool 
&NeedARCStrongQualifier);
      void spaceBeforePlaceHolder(raw_ostream &OS);
-    void printTypeSpec(const NamedDecl *D, raw_ostream &OS);
+    void printTypeSpec(NamedDecl *D, raw_ostream &OS);

      void printBefore(const Type *ty, Qualifiers qs, raw_ostream &OS);
      void printBefore(QualType T, raw_ostream &OS);
@@ -794,7 +794,14 @@
    printAfter(T->getReturnType(), OS);
  }

-void TypePrinter::printTypeSpec(const NamedDecl *D, raw_ostream &OS) {
+void TypePrinter::printTypeSpec(NamedDecl *D, raw_ostream &OS) {
+
+  // Compute the full nested-name-specifier for this type.
+  // In C, this will always be empty except when the type
+  // being printed is anonymous within other Record.
+  if (!Policy.SuppressScope)
+    AppendScope(D->getDeclContext(), OS);
+
    IdentifierInfo *II = D->getIdentifier();
    OS << II->getName();
    spaceBeforePlaceHolder(OS);


Thanks

Michael

On 09/27/2016 02:55 PM, Michael via cfe-users wrote:
> Nobody? Wrong list? Any insight in this would be greatly appreciated.
>
> Thanks
>
> Michael
>
> On 08/30/2016 07:00 PM, Michael via cfe-users wrote:
>> Hi
>>
>> Hope I picked the right place for this kind of problem, if not please
>> let me know.
>>
>> I'm using libclang to parse header files and generate code from them.
>> I found that clang_getTypeSpelling() usually includes the
>> namespace(s) a type was declared in. However with the exception being
>> typedefs (and same for "using A = B"). Not sure if this is a bug or
>> intended behavior, but it seems at least inconsistent. I also
>> couldn't really find a good workaround for this. I'd have to manually
>> figure out all typedefs (not just pure typedefs, they could also be
>> template parameters or whatever) and then their originating
>> namespaces. This sounds a bit cumbersome and not really straight
>> forward.
>>
>> Minimal example:
>>
>> namespace foo {
>> class Bar {
>> };
>> typedef Bar BarDef;
>> }
>>
>> clang_getTypeSpelling on "Bar" (kind "Record") gives: "foo::Bar"
>> clang_getTypeSpelling on "BarDef" (kind "Typedef") gives: "BarDef"
>> (<== missing "foo::")
>>
>> Any idea how to solve this problem? Am I missing something?
>>
>> Thanks
>>
>> Michael
>> _______________________________________________
>> cfe-users mailing list
>> cfe-users at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users
>
> _______________________________________________
> cfe-users mailing list
> cfe-users at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users




More information about the cfe-users mailing list