Skip to content

Commit

Permalink
[cling] Also fwd decl underlying type of using decls:
Browse files Browse the repository at this point in the history
Before, only the using decl itself was forward declared, causing
undeclared identifiers in forward declaration code, as witnessed in
#8499

Given the similarity of using and typedef, merge both into a single
function, making sure both have the same featureset, and through that
fixing this issue as a side-effect.

(cherry picked from commit d026b49)
  • Loading branch information
Axel-Naumann committed Jun 23, 2021
1 parent 90aca00 commit 9a7ea6f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
57 changes: 34 additions & 23 deletions interpreter/cling/lib/Interpreter/ForwardDeclPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ namespace cling {
}
}

void ForwardDeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
void ForwardDeclPrinter::printTypedefOrAliasDecl(TypedefNameDecl *D) {
QualType q = D->getTypeSourceInfo()->getType();
Visit(q);
if (m_SkipFlag) {
Expand All @@ -308,34 +308,45 @@ namespace cling {

std::string closeBraces = PrintEnclosingDeclContexts(Out(),
D->getDeclContext());
if (!m_Policy.SuppressSpecifiers)
Out() << "typedef ";
if (D->isModulePrivate())
Out() << "__module_private__ ";
auto printUnderying = [&]() {
QualType qNoRestrict = q;
if (qNoRestrict.isRestrictQualified())
qNoRestrict.removeLocalRestrict();
qNoRestrict.print(Out(), m_Policy);
};
auto printDeclName = [&]() {
if (D->isModulePrivate())
Out() << "__module_private__ ";

if (q.isRestrictQualified()){
q.removeLocalRestrict();
q.print(Out(), m_Policy, "");
Out() << " __restrict " << D->getName(); //TODO: Find some policy that does this automatically
}
else {
q.print(Out(), m_Policy, D->getName());
if (q.isRestrictQualified()) {
Out() << " __restrict "; // TODO: Find some policy that does this automatically
}
Out() << D->getName();
prettyPrintAttributes(D);
};

if (llvm::isa<TypedefDecl>(D)) {
Out() << "typedef ";
printUnderying();
Out() << " ";
printDeclName();
} else if (llvm::isa<TypeAliasDecl>(D)) {
Out() << "using ";
printDeclName();
Out() << " = ";
printUnderying();
} else {
skipDecl(D, "Neither a typedef nor a type alias!");
}
prettyPrintAttributes(D);
Out() << ';' << closeBraces << '\n';
}

void ForwardDeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
printTypedefOrAliasDecl(D);
}

void ForwardDeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
/*FIXME: Ugly Hack*/
// if(!D->getLexicalDeclContext()->isNamespace()
// && !D->getLexicalDeclContext()->isFileContext())
// return;
std::string closeBraces = PrintEnclosingDeclContexts(Out(),
D->getDeclContext());
Out() << "using " << *D;
prettyPrintAttributes(D);
Out() << " = " << D->getTypeSourceInfo()->getType().getAsString(m_Policy)
<< ';' << closeBraces << '\n';
printTypedefOrAliasDecl(D);
}

void ForwardDeclPrinter::VisitEnumDecl(EnumDecl *D) {
Expand Down
3 changes: 3 additions & 0 deletions interpreter/cling/lib/Interpreter/ForwardDeclPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ namespace clang {
class TranslationUnitDecl;
class TypeAliasDecl;
class TypedefDecl;
class TypedefNameDecl;
class VarDecl;
class UsingDirectiveDecl;
}
Expand Down Expand Up @@ -109,6 +110,8 @@ namespace cling {
std::set<const char*> m_BuiltinNames;
IgnoreFilesFunc_t m_IgnoreFile; // Call back to ignore some top level files.

void printTypedefOrAliasDecl(clang::TypedefNameDecl* D);

public:
ForwardDeclPrinter(llvm::raw_ostream& OutS,
llvm::raw_ostream& LogS,
Expand Down

0 comments on commit 9a7ea6f

Please sign in to comment.