Skip to content

Commit d51a3be

Browse files
committed
[NFC] Make access note diagnostic locations more consistent
Access note diagnostics tend to need a fairly specific “diagnose at attr location if possible, decl location otherwise” pattern; encapsulate that in a helper function and use it everywhere.
1 parent feca4e6 commit d51a3be

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,16 @@ void TypeChecker::diagnoseDuplicateCaptureVars(CaptureListExpr *expr) {
14461446
diagnoseDuplicateDecls(captureListVars);
14471447
}
14481448

1449+
template<typename ...DiagIDAndArgs> InFlightDiagnostic
1450+
diagnoseForAccessNote(DeclAttribute *attr, ValueDecl *VD,
1451+
DiagIDAndArgs... idAndArgs) {
1452+
ASTContext &ctx = VD->getASTContext();
1453+
if (attr->getLocation().isValid())
1454+
return ctx.Diags.diagnose(attr->getLocation(), idAndArgs...);
1455+
else
1456+
return ctx.Diags.diagnose(VD, idAndArgs...);
1457+
}
1458+
14491459
template <typename Attr>
14501460
static void addOrRemoveAttr(ValueDecl *VD, const AccessNotes &notes,
14511461
Optional<bool> expected,
@@ -1459,20 +1469,10 @@ static void addOrRemoveAttr(ValueDecl *VD, const AccessNotes &notes,
14591469
[&](Diag<StringRef, bool, StringRef, DescriptiveDeclKind> diagID,
14601470
Diag<bool> fixitID) -> InFlightDiagnostic {
14611471
bool isModifier = attr->isDeclModifier();
1462-
Diagnostic warning(diagID, notes.reason, isModifier, attr->getAttrName(),
1463-
VD->getDescriptiveKind());
1464-
Diagnostic note(fixitID, isModifier);
14651472

1466-
ASTContext &ctx = VD->getASTContext();
1467-
1468-
if (attr->getLocation().isValid()) {
1469-
ctx.Diags.diagnose(attr->getLocation(), warning);
1470-
return ctx.Diags.diagnose(attr->getLocation(), note);
1471-
}
1472-
else {
1473-
ctx.Diags.diagnose(VD, warning);
1474-
return ctx.Diags.diagnose(VD->getAttributeInsertionLoc(isModifier), note);
1475-
}
1473+
diagnoseForAccessNote(attr, VD, diagID, notes.reason, isModifier,
1474+
attr->getAttrName(), VD->getDescriptiveKind());
1475+
return diagnoseForAccessNote(attr, VD, fixitID, isModifier);
14761476
};
14771477

14781478
if (*expected) {
@@ -1513,20 +1513,19 @@ static void applyAccessNote(ValueDecl *VD, const AccessNote &note,
15131513

15141514
if (!attr->hasName()) {
15151515
attr->setName(*note.ObjCName, true);
1516-
ctx.Diags.diagnose(attr->getLocation(),
1517-
diag::attr_objc_name_changed_by_access_note,
1518-
notes.reason, VD->getDescriptiveKind(),
1519-
*note.ObjCName);
1516+
diagnoseForAccessNote(attr, VD,
1517+
diag::attr_objc_name_changed_by_access_note,
1518+
notes.reason, VD->getDescriptiveKind(),
1519+
*note.ObjCName);
15201520

15211521
SmallString<64> newNameString;
15221522
llvm::raw_svector_ostream os(newNameString);
15231523
os << "(";
15241524
os << *note.ObjCName;
15251525
os << ")";
15261526

1527-
auto note =
1528-
ctx.Diags.diagnose(attr->getLocation(),
1529-
diag::fixit_attr_objc_name_changed_by_access_note);
1527+
auto note = diagnoseForAccessNote(attr, VD,
1528+
diag::fixit_attr_objc_name_changed_by_access_note);
15301529

15311530
if (attr->getLParenLoc().isValid())
15321531
note.fixItReplace({ attr->getLParenLoc(), attr->getRParenLoc() },
@@ -1537,6 +1536,7 @@ static void applyAccessNote(ValueDecl *VD, const AccessNote &note,
15371536
else if (attr->getName() != *note.ObjCName) {
15381537
// TODO: diagnose conflict between explicit name in code and explicit
15391538
// name in access note
1539+
llvm::report_fatal_error("conflict between name in code and name in access note");
15401540
}
15411541
}
15421542
}

0 commit comments

Comments
 (0)