Skip to content

Commit 6d9a3a1

Browse files
Code review
Fix naming and comment
1 parent 087f982 commit 6d9a3a1

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

clang/include/clang/AST/ASTImporter.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,11 @@ class TypeSourceInfo;
256256
/// Declaration (from, to) pairs that are known not to be equivalent
257257
/// (which we have already complained about).
258258
NonEquivalentDeclSet NonEquivalentDecls;
259-
// When template function return type is auto and return type is declared as
260-
// typename from template params, there could be cycles in function
261-
// importing when function decaration is still the need for return type
262-
// declaration import. We have code path for nested types inside function
263-
// (see hasReturnTypeDeclaredInside): assuming return type as VoidTy and
264-
// calculate it later under UsedDifferentProtoType boolean. This class is
265-
// reuse of this approach and make logic lazy - detect cycle - calculate
266-
// return type later on.
259+
/// A FunctionDecl can have properties that have a reference to the
260+
/// function itself and are imported before the function is created. This
261+
/// can come for example from auto return type or when template parameters
262+
/// are used in the return type or parameters. This member is used to detect
263+
/// cyclic import of FunctionDecl objects to avoid infinite recursion.
267264
std::unique_ptr<FunctionReturnTypeDeclCycleDetector>
268265
FunctionReturnTypeCycleDetector;
269266

clang/lib/AST/ASTImporter.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,34 +1289,34 @@ using namespace clang;
12891289

12901290
class ASTImporter::FunctionReturnTypeDeclCycleDetector {
12911291
public:
1292-
class ScopedReturnTypeImport {
1292+
class DeclCycleMapInserter {
12931293
public:
12941294
// Do not track cycles on D == nullptr.
1295-
ScopedReturnTypeImport(FunctionReturnTypeDeclCycleDetector &owner,
1296-
const FunctionDecl *D)
1295+
DeclCycleMapInserter(FunctionReturnTypeDeclCycleDetector &owner,
1296+
const FunctionDecl *D)
12971297
: CycleDetector(owner), D(D) {
12981298
if (D)
12991299
CycleDetector.FunctionReturnTypeDeclCycles.insert(D);
13001300
}
1301-
~ScopedReturnTypeImport() {
1301+
~DeclCycleMapInserter() {
13021302
if (D)
13031303
CycleDetector.FunctionReturnTypeDeclCycles.erase(D);
13041304
}
1305-
ScopedReturnTypeImport(const ScopedReturnTypeImport &) = delete;
1306-
ScopedReturnTypeImport &operator=(const ScopedReturnTypeImport &) = delete;
1305+
DeclCycleMapInserter(const DeclCycleMapInserter &) = delete;
1306+
DeclCycleMapInserter &operator=(const DeclCycleMapInserter &) = delete;
13071307

13081308
private:
13091309
FunctionReturnTypeDeclCycleDetector &CycleDetector;
13101310
const FunctionDecl *D;
13111311
};
13121312

1313-
ScopedReturnTypeImport DetectImportCycles(const FunctionDecl *D) {
1314-
if (!IsCycle(D))
1315-
return ScopedReturnTypeImport(*this, D);
1316-
return ScopedReturnTypeImport(*this, nullptr);
1313+
DeclCycleMapInserter detectImportCycle(const FunctionDecl *D) {
1314+
if (!isCycle(D))
1315+
return DeclCycleMapInserter(*this, D);
1316+
return DeclCycleMapInserter(*this, nullptr);
13171317
}
13181318

1319-
bool IsCycle(const FunctionDecl *D) const {
1319+
bool isCycle(const FunctionDecl *D) const {
13201320
return FunctionReturnTypeDeclCycles.find(D) !=
13211321
FunctionReturnTypeDeclCycles.end();
13221322
}
@@ -4076,7 +4076,7 @@ ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
40764076
// Reuse this approach for auto return types declared as typenames from
40774077
// template pamams, tracked in FunctionReturnTypeCycleDetector.
40784078
if (hasReturnTypeDeclaredInside(D) ||
4079-
Importer.FunctionReturnTypeCycleDetector->IsCycle(D)) {
4079+
Importer.FunctionReturnTypeCycleDetector->isCycle(D)) {
40804080
FromReturnTy = Importer.getFromContext().VoidTy;
40814081
UsedDifferentProtoType = true;
40824082
}
@@ -4100,7 +4100,7 @@ ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
41004100

41014101
Error Err = Error::success();
41024102
auto ScopedReturnTypeDeclCycleDetector =
4103-
Importer.FunctionReturnTypeCycleDetector->DetectImportCycles(D);
4103+
Importer.FunctionReturnTypeCycleDetector->detectImportCycle(D);
41044104
auto T = importChecked(Err, FromTy);
41054105
auto TInfo = importChecked(Err, FromTSI);
41064106
auto ToInnerLocStart = importChecked(Err, D->getInnerLocStart());

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3204,7 +3204,7 @@ TEST_P(ImportExpr, UnresolvedMemberExpr) {
32043204
compoundStmt(has(callExpr(has(unresolvedMemberExpr())))))))));
32053205
}
32063206

3207-
TEST_P(ImportExpr, CycleInAutoTemplateSpec) {
3207+
TEST_P(ImportDecl, CycleInAutoTemplateSpec) {
32083208
MatchVerifier<Decl> Verifier;
32093209
const char *Code = R"(
32103210
template <class _CharT>

0 commit comments

Comments
 (0)