Skip to content

Commit 68b50af

Browse files
committed
exporter: store SourceLocation/SourceRange in TypeEncoder::VisitQualTypeOf to use in warnings
1 parent 608a14b commit 68b50af

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

c2rust-ast-exporter/src/AstExporter.cpp

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ class TypeEncoder final : public TypeVisitor<TypeEncoder> {
167167

168168
std::unordered_set<const clang::Type *> exports;
169169

170+
public:
171+
/// Set before `TypeEncoder::VisitQualType(ty)` in `TypeEncoder::VisitQualTypeOf`.
172+
SourceLocation src_loc;
173+
SourceRange src_range;
174+
175+
private:
170176
bool markExported(const clang::Type *ptr) {
171177
return exports.emplace(ptr).second;
172178
}
@@ -225,6 +231,13 @@ class TypeEncoder final : public TypeVisitor<TypeEncoder> {
225231
: Context(Context), encoder(encoder), sugared(sugared),
226232
astEncoder(ast) {}
227233

234+
template <class T> // Usually `Decl`, `Expr`, or `Stmt`.
235+
void VisitQualTypeOf(const QualType &QT, const T *t) {
236+
src_loc = getSourceLocation(t);
237+
src_range = t->getSourceRange();
238+
VisitQualType(QT);
239+
}
240+
228241
void VisitQualType(const QualType &QT) {
229242
if (!QT.isNull()) {
230243
auto s = QT.split();
@@ -461,7 +474,7 @@ class TypeEncoder final : public TypeVisitor<TypeEncoder> {
461474
auto warning = std::string("Encountered unsupported BuiltinType kind ") +
462475
std::to_string((int)kind) + " for type " +
463476
T->getName(pol).str();
464-
printDiag(Context, DiagnosticsEngine::Warning, warning, clang::FullSourceLoc());
477+
printDiag(Context, DiagnosticsEngine::Warning, warning, src_loc, src_range);
465478
return TagTypeUnknown;
466479
}
467480
}();
@@ -735,7 +748,7 @@ class TranslateASTVisitor final
735748
#endif
736749
encode_entry_raw(ast, tag, ast->getSourceRange(), ty, isRValue, isVaList,
737750
encodeMacroExpansions, childIds, extra);
738-
typeEncoder.VisitQualType(ty);
751+
typeEncoder.VisitQualTypeOf(ty, ast);
739752
}
740753

741754
void encode_entry(
@@ -821,7 +834,7 @@ class TranslateASTVisitor final
821834
else if (info.Name != name)
822835
return false;
823836

824-
typeEncoder.VisitQualType(E->getType());
837+
typeEncoder.VisitQualTypeOf(E->getType(), E);
825838
return true;
826839
}
827840

@@ -1398,7 +1411,7 @@ class TranslateASTVisitor final
13981411
}
13991412
cbor_encode_uint(extras, qt);
14001413
});
1401-
typeEncoder.VisitQualType(t);
1414+
typeEncoder.VisitQualTypeOf(t, E);
14021415
return true;
14031416
}
14041417

@@ -1449,7 +1462,7 @@ class TranslateASTVisitor final
14491462
// gets visited.
14501463
if (!value) {
14511464
auto ty = E->getTypeSourceInfo()->getType();
1452-
typeEncoder.VisitQualType(ty);
1465+
typeEncoder.VisitQualTypeOf(ty, E);
14531466
}
14541467

14551468
return true;
@@ -1700,8 +1713,8 @@ class TranslateASTVisitor final
17001713
if (auto cao = dyn_cast_or_null<CompoundAssignOperator>(BO)) {
17011714
computationLHSType = cao->getComputationLHSType();
17021715
computationResultType = cao->getComputationResultType();
1703-
typeEncoder.VisitQualType(computationLHSType);
1704-
typeEncoder.VisitQualType(computationResultType);
1716+
typeEncoder.VisitQualTypeOf(computationLHSType, cao);
1717+
typeEncoder.VisitQualTypeOf(computationResultType, cao);
17051718
}
17061719

17071720
encode_entry(BO, TagBinaryOperator, childIds,
@@ -1886,7 +1899,7 @@ class TranslateASTVisitor final
18861899
if (FD->doesThisDeclarationHaveABody())
18871900
span = FD->getCanonicalDecl()->getSourceRange();
18881901
encode_entry(FD, TagNonCanonicalDecl, span, childIds, FD->getType());
1889-
typeEncoder.VisitQualType(FD->getType());
1902+
typeEncoder.VisitQualTypeOf(FD->getType(), FD);
18901903
return true;
18911904
}
18921905

@@ -1981,7 +1994,7 @@ class TranslateASTVisitor final
19811994

19821995
cbor_encoder_close_container(array, &attr_info);
19831996
});
1984-
typeEncoder.VisitQualType(functionType);
1997+
typeEncoder.VisitQualTypeOf(functionType, paramsFD);
19851998

19861999
return true;
19872000
}
@@ -2008,7 +2021,7 @@ class TranslateASTVisitor final
20082021
// Emit non-canonical decl so we have a placeholder to attach comments to
20092022
std::vector<void *> childIds = {VD->getCanonicalDecl()};
20102023
encode_entry(VD, TagNonCanonicalDecl, VD->getLocation(), childIds, VD->getType());
2011-
typeEncoder.VisitQualType(VD->getType());
2024+
typeEncoder.VisitQualTypeOf(VD->getType(), VD);
20122025
return true;
20132026
}
20142027

@@ -2088,7 +2101,7 @@ class TranslateASTVisitor final
20882101
cbor_encoder_close_container(array, &attr_info);
20892102
});
20902103

2091-
typeEncoder.VisitQualType(T);
2104+
typeEncoder.VisitQualTypeOf(T, def);
20922105

20932106
return true;
20942107
}
@@ -2213,7 +2226,7 @@ class TranslateASTVisitor final
22132226
}
22142227

22152228
auto underlying_type = D->getIntegerType();
2216-
typeEncoder.VisitQualType(underlying_type);
2229+
typeEncoder.VisitQualTypeOf(underlying_type, D);
22172230

22182231
encode_entry(D, TagEnumDecl, childIds, underlying_type,
22192232
[D](CborEncoder *local) {
@@ -2259,7 +2272,7 @@ class TranslateASTVisitor final
22592272
// Emit non-canonical decl so we have a placeholder to attach comments to
22602273
std::vector<void *> childIds = {D->getCanonicalDecl()};
22612274
encode_entry(D, TagNonCanonicalDecl, D->getLocation(), childIds, D->getType());
2262-
typeEncoder.VisitQualType(D->getType());
2275+
typeEncoder.VisitQualTypeOf(D->getType(), D);
22632276
return true;
22642277
}
22652278

@@ -2299,7 +2312,7 @@ class TranslateASTVisitor final
22992312
});
23002313

23012314
// This might be the only occurrence of this type in the translation unit
2302-
typeEncoder.VisitQualType(t);
2315+
typeEncoder.VisitQualTypeOf(t, D);
23032316

23042317
return true;
23052318
}
@@ -2342,7 +2355,7 @@ class TranslateASTVisitor final
23422355
// Emit non-canonical decl so we have a placeholder to attach comments to
23432356
std::vector<void *> childIds = {D->getCanonicalDecl()};
23442357
encode_entry(D, TagNonCanonicalDecl, D->getLocation(), childIds, typeForDecl);
2345-
typeEncoder.VisitQualType(typeForDecl);
2358+
typeEncoder.VisitQualTypeOf(typeForDecl, D);
23462359
return true;
23472360
}
23482361

@@ -2361,7 +2374,7 @@ class TranslateASTVisitor final
23612374
}
23622375
});
23632376

2364-
typeEncoder.VisitQualType(typeForDecl);
2377+
typeEncoder.VisitQualTypeOf(typeForDecl, D);
23652378

23662379
return true;
23672380
}

0 commit comments

Comments
 (0)