llvm-project
80 строк · 2.6 Кб
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
14#include "clang/ExtractAPI/TypedefUnderlyingTypeResolver.h"15#include "clang/Basic/Module.h"16#include "clang/Index/USRGeneration.h"17
18using namespace clang;19using namespace extractapi;20
21const NamedDecl *22TypedefUnderlyingTypeResolver::getUnderlyingTypeDecl(QualType Type) const {23const NamedDecl *TypeDecl = nullptr;24
25const TypedefType *TypedefTy = Type->getAs<TypedefType>();26if (TypedefTy)27TypeDecl = TypedefTy->getDecl();28if (const TagType *TagTy = Type->getAs<TagType>()) {29TypeDecl = TagTy->getDecl();30} else if (const ObjCInterfaceType *ObjCITy =31Type->getAs<ObjCInterfaceType>()) {32TypeDecl = ObjCITy->getDecl();33}34
35if (TypeDecl && TypedefTy) {36// if this is a typedef to another typedef, use the typedef's decl for the37// USR - this will actually be in the output, unlike a typedef to an38// anonymous decl39const TypedefNameDecl *TypedefDecl = TypedefTy->getDecl();40if (TypedefDecl->getUnderlyingType()->isTypedefNameType())41TypeDecl = TypedefDecl;42}43
44return TypeDecl;45}
46
47SymbolReference
48TypedefUnderlyingTypeResolver::getSymbolReferenceForType(QualType Type,49APISet &API) const {50std::string TypeName = Type.getAsString();51SmallString<128> TypeUSR;52const NamedDecl *TypeDecl = getUnderlyingTypeDecl(Type);53const TypedefType *TypedefTy = Type->getAs<TypedefType>();54StringRef OwningModuleName;55
56if (TypeDecl) {57if (!TypedefTy)58TypeName = TypeDecl->getName().str();59
60clang::index::generateUSRForDecl(TypeDecl, TypeUSR);61if (auto *OwningModule = TypeDecl->getImportedOwningModule())62OwningModuleName = OwningModule->Name;63} else {64clang::index::generateUSRForType(Type, Context, TypeUSR);65}66
67return API.createSymbolReference(TypeName, TypeUSR, OwningModuleName);68}
69
70std::string TypedefUnderlyingTypeResolver::getUSRForType(QualType Type) const {71SmallString<128> TypeUSR;72const NamedDecl *TypeDecl = getUnderlyingTypeDecl(Type);73
74if (TypeDecl)75clang::index::generateUSRForDecl(TypeDecl, TypeUSR);76else77clang::index::generateUSRForType(Type, Context, TypeUSR);78
79return std::string(TypeUSR);80}
81