|
22 | 22 | #include "clang/Basic/SourceManager.h"
|
23 | 23 | #include "clang/ExtractAPI/API.h"
|
24 | 24 | #include "clang/ExtractAPI/TypedefUnderlyingTypeResolver.h"
|
| 25 | +#include "llvm/ADT/StringRef.h" |
25 | 26 | #include <type_traits>
|
26 | 27 |
|
27 | 28 | namespace clang {
|
@@ -105,6 +106,24 @@ class ExtractAPIVisitorBase : public RecursiveASTVisitor<Derived> {
|
105 | 106 | }
|
106 | 107 | };
|
107 | 108 |
|
| 109 | +template <typename T> |
| 110 | +static void modifyRecords(const T &Records, const StringRef &Name) { |
| 111 | + for (const auto &Record : Records) { |
| 112 | + if (Name == Record.second.get()->Name) { |
| 113 | + Record.second.get()->Declaration.removeLast(); |
| 114 | + Record.second.get() |
| 115 | + ->Declaration |
| 116 | + .appendFront(" ", DeclarationFragments::FragmentKind::Text) |
| 117 | + .appendFront("typedef", DeclarationFragments::FragmentKind::Keyword, |
| 118 | + "", nullptr) |
| 119 | + .append(" { ... } ", DeclarationFragments::FragmentKind::Text) |
| 120 | + .append(Name, DeclarationFragments::FragmentKind::Identifier) |
| 121 | + .append(";", DeclarationFragments::FragmentKind::Text); |
| 122 | + break; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
108 | 127 | template <typename Derived>
|
109 | 128 | bool ExtractAPIVisitorBase<Derived>::VisitVarDecl(const VarDecl *Decl) {
|
110 | 129 | // skip function parameters.
|
@@ -401,6 +420,21 @@ bool ExtractAPIVisitorBase<Derived>::VisitTypedefNameDecl(
|
401 | 420 | if (!getDerivedExtractAPIVisitor().shouldDeclBeIncluded(Decl))
|
402 | 421 | return true;
|
403 | 422 |
|
| 423 | + // Add the notion of typedef for tag type (struct or enum) of the same name. |
| 424 | + if (const ElaboratedType *ET = |
| 425 | + dyn_cast<ElaboratedType>(Decl->getUnderlyingType())) { |
| 426 | + if (const TagType *TagTy = dyn_cast<TagType>(ET->desugar())) { |
| 427 | + if (Decl->getName() == TagTy->getDecl()->getName()) { |
| 428 | + if (TagTy->getDecl()->isStruct()) { |
| 429 | + modifyRecords(API.getStructs(), Decl->getName()); |
| 430 | + } |
| 431 | + if (TagTy->getDecl()->isEnum()) { |
| 432 | + modifyRecords(API.getEnums(), Decl->getName()); |
| 433 | + } |
| 434 | + } |
| 435 | + } |
| 436 | + } |
| 437 | + |
404 | 438 | PresumedLoc Loc =
|
405 | 439 | Context.getSourceManager().getPresumedLoc(Decl->getLocation());
|
406 | 440 | StringRef Name = Decl->getName();
|
|
0 commit comments