[clang] [AST] Add dump() method to TypeLoc (PR #65484)

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 11 13:49:18 PDT 2023


================
@@ -415,9 +443,55 @@ class ASTNodeTraverser
     if (!T->isSugared())
       Visit(T->getPattern());
   }
+  void VisitAutoType(const AutoType *T) {
+    for (const auto &Arg : T->getTypeConstraintArguments())
+      Visit(Arg);
+  }
   // FIXME: ElaboratedType, DependentNameType,
   // DependentTemplateSpecializationType, ObjCObjectType
 
+  // For TypeLocs, we automatically visit the inner type loc (pointee type etc).
+  // We must explicitly visit other lexically-nested nodes.
+  void VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
----------------
sam-mccall wrote:

> Do we need to explicitly visit the return type?

No, the inner/next type loc of a FunctionTypeLoc is the return type.

> function declaration with a regular return type

This one is already there

> one with a trailing return type

Added. The trailing return type is visited as the inner type loc. AFAICT the AST has no TypeLoc for the `auto` in `auto x() -> int`.

> another test with a lambda expression

I'm not sure what such a test should do.
`[](){}` doesn't really have meaningful TypeLocs, and `[]()->int{}` just has the BuiltinTypeLoc for int.
Do you want a dump of the (implicit) call operator's typeloc?

> good to have a test case involving a function template

added

> What about things like exception specifications? e.g., void func() throw(int);

Added.
Current behavior is to print the exception types, with no location information.
The AST does not have a TypeLoc for `int` here. It does have a SourceRange for the exception spec, which we could potentially include.
(I don't think this is common+useful enough to do in this initial patch though)

https://github.com/llvm/llvm-project/pull/65484


More information about the cfe-commits mailing list