clang  16.0.0git
TypedefUnderlyingTypeResolver.cpp
Go to the documentation of this file.
1 //===- ExtractAPI/TypedefUnderlyingTypeResolver.cpp -------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file implements UnderlyingTypeResolver.
11 ///
12 //===----------------------------------------------------------------------===//
13 
16 
17 using namespace clang;
18 using namespace extractapi;
19 
20 namespace {
21 
22 const NamedDecl *getUnderlyingTypeDecl(QualType Type) {
23  const NamedDecl *TypeDecl = nullptr;
24 
25  const TypedefType *TypedefTy = Type->getAs<TypedefType>();
26  if (TypedefTy)
27  TypeDecl = TypedefTy->getDecl();
28  if (const TagType *TagTy = Type->getAs<TagType>()) {
29  TypeDecl = TagTy->getDecl();
30  } else if (const ObjCInterfaceType *ObjCITy =
32  TypeDecl = ObjCITy->getDecl();
33  }
34 
35  if (TypeDecl && TypedefTy) {
36  // if this is a typedef to another typedef, use the typedef's decl for the
37  // USR - this will actually be in the output, unlike a typedef to an
38  // anonymous decl
39  const TypedefNameDecl *TypedefDecl = TypedefTy->getDecl();
42  }
43 
44  return TypeDecl;
45 }
46 
47 } // namespace
48 
51  APISet &API) const {
52  std::string TypeName = Type.getAsString();
53  SmallString<128> TypeUSR;
54  const NamedDecl *TypeDecl = getUnderlyingTypeDecl(Type);
55  const TypedefType *TypedefTy = Type->getAs<TypedefType>();
56 
57  if (TypeDecl) {
58  if (!TypedefTy)
59  TypeName = TypeDecl->getName().str();
60 
62  } else {
63  clang::index::generateUSRForType(Type, Context, TypeUSR);
64  }
65 
66  return {API.copyString(TypeName), API.copyString(TypeUSR)};
67 }
68 
70  SmallString<128> TypeUSR;
71  const NamedDecl *TypeDecl = getUnderlyingTypeDecl(Type);
72 
73  if (TypeDecl)
75  else
76  clang::index::generateUSRForType(Type, Context, TypeUSR);
77 
78  return std::string(TypeUSR);
79 }
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::NamedDecl
This represents a decl that may have a name.
Definition: Decl.h:247
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:737
clang::TagType
Definition: Type.h:4792
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1565
clang::TypedefType
Definition: Type.h:4535
clang::Type::getAs
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:7386
clang::extractapi::APISet::copyString
StringRef copyString(StringRef String)
Copy String into the Allocator in this APISet.
Definition: API.cpp:213
clang::TypedefNameDecl::getUnderlyingType
QualType getUnderlyingType() const
Definition: Decl.h:3330
llvm::SmallString< 128 >
clang::TypeDecl
Represents a declaration of a type.
Definition: Decl.h:3233
clang::extractapi::TypedefUnderlyingTypeResolver::getSymbolReferenceForType
SymbolReference getSymbolReferenceForType(QualType Type, APISet &API) const
Get a SymbolReference for the given type.
Definition: TypedefUnderlyingTypeResolver.cpp:50
clang::TypedefType::getDecl
TypedefNameDecl * getDecl() const
Definition: Type.h:4546
clang::extractapi::TypedefUnderlyingTypeResolver::getUSRForType
std::string getUSRForType(QualType Type) const
Get a USR for the given type.
Definition: TypedefUnderlyingTypeResolver.cpp:69
USRGeneration.h
clang::TypedefDecl
Represents the declaration of a typedef-name via the 'typedef' type specifier.
Definition: Decl.h:3377
clang::DeclaratorContext::TypeName
@ TypeName
clang::index::generateUSRForDecl
bool generateUSRForDecl(const Decl *D, SmallVectorImpl< char > &Buf)
Generate a USR for a Decl, including the USR prefix.
Definition: USRGeneration.cpp:1089
clang
Definition: CalledOnceCheck.h:17
clang::extractapi::APISet
APISet holds the set of API records collected from given inputs.
Definition: API.h:469
clang::Type::isTypedefNameType
bool isTypedefNameType() const
Determines whether this type is written as a typedef-name.
Definition: Type.h:7314
clang::TypedefNameDecl
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:3275
TypedefUnderlyingTypeResolver.h
clang::index::generateUSRForType
bool generateUSRForType(QualType T, ASTContext &Ctx, SmallVectorImpl< char > &Buf)
Generates a USR for a type.
Definition: USRGeneration.cpp:1132
clang::ObjCInterfaceType
Interfaces are the core concept in Objective-C for object oriented design.
Definition: Type.h:6214
clang::extractapi::SymbolReference
This represents a reference to another symbol that might come from external sources.
Definition: API.h:313
clang::NamedDecl::getName
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:274