|
9 | 9 | #include "IdentifierNamingCheck.h"
|
10 | 10 |
|
11 | 11 | #include "../GlobList.h"
|
| 12 | +#include "../utils/ASTUtils.h" |
12 | 13 | #include "clang/AST/CXXInheritance.h"
|
13 | 14 | #include "clang/Lex/PPCallbacks.h"
|
14 | 15 | #include "clang/Lex/Preprocessor.h"
|
@@ -286,7 +287,9 @@ IdentifierNamingCheck::FileStyle IdentifierNamingCheck::getFileStyleFromOptions(
|
286 | 287 | HPTOpt.value_or(IdentifierNamingCheck::HPT_Off));
|
287 | 288 | }
|
288 | 289 | bool IgnoreMainLike = Options.get("IgnoreMainLikeFunctions", false);
|
289 |
| - return {std::move(Styles), std::move(HNOption), IgnoreMainLike}; |
| 290 | + bool CheckAnonFieldInParent = Options.get("CheckAnonFieldInParent", false); |
| 291 | + return {std::move(Styles), std::move(HNOption), IgnoreMainLike, |
| 292 | + CheckAnonFieldInParent}; |
290 | 293 | }
|
291 | 294 |
|
292 | 295 | std::string IdentifierNamingCheck::HungarianNotation::getDeclTypeName(
|
@@ -859,6 +862,8 @@ void IdentifierNamingCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
|
859 | 862 | Options.store(Opts, "IgnoreFailedSplit", IgnoreFailedSplit);
|
860 | 863 | Options.store(Opts, "IgnoreMainLikeFunctions",
|
861 | 864 | MainFileStyle->isIgnoringMainLikeFunction());
|
| 865 | + Options.store(Opts, "CheckAnonFieldInParent", |
| 866 | + MainFileStyle->isCheckingAnonFieldInParentScope()); |
862 | 867 | }
|
863 | 868 |
|
864 | 869 | bool IdentifierNamingCheck::matchesStyle(
|
@@ -1111,7 +1116,7 @@ std::string IdentifierNamingCheck::fixupWithStyle(
|
1111 | 1116 | StyleKind IdentifierNamingCheck::findStyleKind(
|
1112 | 1117 | const NamedDecl *D,
|
1113 | 1118 | ArrayRef<std::optional<IdentifierNamingCheck::NamingStyle>> NamingStyles,
|
1114 |
| - bool IgnoreMainLikeFunctions) const { |
| 1119 | + bool IgnoreMainLikeFunctions, bool CheckAnonFieldInParentScope) const { |
1115 | 1120 | assert(D && D->getIdentifier() && !D->getName().empty() && !D->isImplicit() &&
|
1116 | 1121 | "Decl must be an explicit identifier with a name.");
|
1117 | 1122 |
|
@@ -1185,29 +1190,14 @@ StyleKind IdentifierNamingCheck::findStyleKind(
|
1185 | 1190 | }
|
1186 | 1191 |
|
1187 | 1192 | if (const auto *Decl = dyn_cast<FieldDecl>(D)) {
|
1188 |
| - QualType Type = Decl->getType(); |
1189 |
| - |
1190 |
| - if (!Type.isNull() && Type.isConstQualified()) { |
1191 |
| - if (NamingStyles[SK_ConstantMember]) |
1192 |
| - return SK_ConstantMember; |
1193 |
| - |
1194 |
| - if (NamingStyles[SK_Constant]) |
1195 |
| - return SK_Constant; |
| 1193 | + if (CheckAnonFieldInParentScope) { |
| 1194 | + const RecordDecl *Record = Decl->getParent(); |
| 1195 | + if (Record->isAnonymousStructOrUnion()) { |
| 1196 | + return findStyleKindForAnonField(Decl, NamingStyles); |
| 1197 | + } |
1196 | 1198 | }
|
1197 | 1199 |
|
1198 |
| - if (Decl->getAccess() == AS_private && NamingStyles[SK_PrivateMember]) |
1199 |
| - return SK_PrivateMember; |
1200 |
| - |
1201 |
| - if (Decl->getAccess() == AS_protected && NamingStyles[SK_ProtectedMember]) |
1202 |
| - return SK_ProtectedMember; |
1203 |
| - |
1204 |
| - if (Decl->getAccess() == AS_public && NamingStyles[SK_PublicMember]) |
1205 |
| - return SK_PublicMember; |
1206 |
| - |
1207 |
| - if (NamingStyles[SK_Member]) |
1208 |
| - return SK_Member; |
1209 |
| - |
1210 |
| - return SK_Invalid; |
| 1200 | + return findStyleKindForField(Decl, Decl->getType(), NamingStyles); |
1211 | 1201 | }
|
1212 | 1202 |
|
1213 | 1203 | if (const auto *Decl = dyn_cast<ParmVarDecl>(D)) {
|
@@ -1244,66 +1234,7 @@ StyleKind IdentifierNamingCheck::findStyleKind(
|
1244 | 1234 | }
|
1245 | 1235 |
|
1246 | 1236 | if (const auto *Decl = dyn_cast<VarDecl>(D)) {
|
1247 |
| - QualType Type = Decl->getType(); |
1248 |
| - |
1249 |
| - if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable]) |
1250 |
| - return SK_ConstexprVariable; |
1251 |
| - |
1252 |
| - if (!Type.isNull() && Type.isConstQualified()) { |
1253 |
| - if (Decl->isStaticDataMember() && NamingStyles[SK_ClassConstant]) |
1254 |
| - return SK_ClassConstant; |
1255 |
| - |
1256 |
| - if (Decl->isFileVarDecl() && Type.getTypePtr()->isAnyPointerType() && |
1257 |
| - NamingStyles[SK_GlobalConstantPointer]) |
1258 |
| - return SK_GlobalConstantPointer; |
1259 |
| - |
1260 |
| - if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalConstant]) |
1261 |
| - return SK_GlobalConstant; |
1262 |
| - |
1263 |
| - if (Decl->isStaticLocal() && NamingStyles[SK_StaticConstant]) |
1264 |
| - return SK_StaticConstant; |
1265 |
| - |
1266 |
| - if (Decl->isLocalVarDecl() && Type.getTypePtr()->isAnyPointerType() && |
1267 |
| - NamingStyles[SK_LocalConstantPointer]) |
1268 |
| - return SK_LocalConstantPointer; |
1269 |
| - |
1270 |
| - if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalConstant]) |
1271 |
| - return SK_LocalConstant; |
1272 |
| - |
1273 |
| - if (Decl->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalConstant]) |
1274 |
| - return SK_LocalConstant; |
1275 |
| - |
1276 |
| - if (NamingStyles[SK_Constant]) |
1277 |
| - return SK_Constant; |
1278 |
| - } |
1279 |
| - |
1280 |
| - if (Decl->isStaticDataMember() && NamingStyles[SK_ClassMember]) |
1281 |
| - return SK_ClassMember; |
1282 |
| - |
1283 |
| - if (Decl->isFileVarDecl() && Type.getTypePtr()->isAnyPointerType() && |
1284 |
| - NamingStyles[SK_GlobalPointer]) |
1285 |
| - return SK_GlobalPointer; |
1286 |
| - |
1287 |
| - if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalVariable]) |
1288 |
| - return SK_GlobalVariable; |
1289 |
| - |
1290 |
| - if (Decl->isStaticLocal() && NamingStyles[SK_StaticVariable]) |
1291 |
| - return SK_StaticVariable; |
1292 |
| - |
1293 |
| - if (Decl->isLocalVarDecl() && Type.getTypePtr()->isAnyPointerType() && |
1294 |
| - NamingStyles[SK_LocalPointer]) |
1295 |
| - return SK_LocalPointer; |
1296 |
| - |
1297 |
| - if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalVariable]) |
1298 |
| - return SK_LocalVariable; |
1299 |
| - |
1300 |
| - if (Decl->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalVariable]) |
1301 |
| - return SK_LocalVariable; |
1302 |
| - |
1303 |
| - if (NamingStyles[SK_Variable]) |
1304 |
| - return SK_Variable; |
1305 |
| - |
1306 |
| - return SK_Invalid; |
| 1237 | + return findStyleKindForVar(Decl, Decl->getType(), NamingStyles); |
1307 | 1238 | }
|
1308 | 1239 |
|
1309 | 1240 | if (const auto *Decl = dyn_cast<CXXMethodDecl>(D)) {
|
@@ -1442,12 +1373,13 @@ IdentifierNamingCheck::getDeclFailureInfo(const NamedDecl *Decl,
|
1442 | 1373 | if (!FileStyle.isActive())
|
1443 | 1374 | return std::nullopt;
|
1444 | 1375 |
|
1445 |
| - return getFailureInfo(HungarianNotation.getDeclTypeName(Decl), |
1446 |
| - Decl->getName(), Decl, Loc, FileStyle.getStyles(), |
1447 |
| - FileStyle.getHNOption(), |
1448 |
| - findStyleKind(Decl, FileStyle.getStyles(), |
1449 |
| - FileStyle.isIgnoringMainLikeFunction()), |
1450 |
| - SM, IgnoreFailedSplit); |
| 1376 | + return getFailureInfo( |
| 1377 | + HungarianNotation.getDeclTypeName(Decl), Decl->getName(), Decl, Loc, |
| 1378 | + FileStyle.getStyles(), FileStyle.getHNOption(), |
| 1379 | + findStyleKind(Decl, FileStyle.getStyles(), |
| 1380 | + FileStyle.isIgnoringMainLikeFunction(), |
| 1381 | + FileStyle.isCheckingAnonFieldInParentScope()), |
| 1382 | + SM, IgnoreFailedSplit); |
1451 | 1383 | }
|
1452 | 1384 |
|
1453 | 1385 | std::optional<RenamerClangTidyCheck::FailureInfo>
|
@@ -1496,5 +1428,114 @@ IdentifierNamingCheck::getStyleForFile(StringRef FileName) const {
|
1496 | 1428 | return It.first->getValue();
|
1497 | 1429 | }
|
1498 | 1430 |
|
| 1431 | +StyleKind IdentifierNamingCheck::findStyleKindForAnonField( |
| 1432 | + const FieldDecl *AnonField, |
| 1433 | + ArrayRef<std::optional<NamingStyle>> NamingStyles) const { |
| 1434 | + const IndirectFieldDecl *IFD = |
| 1435 | + utils::findOutermostIndirectFieldDeclForField(AnonField); |
| 1436 | + assert(IFD && "Found an anonymous record field without an IndirectFieldDecl"); |
| 1437 | + |
| 1438 | + QualType Type = AnonField->getType(); |
| 1439 | + |
| 1440 | + if (const auto *F = dyn_cast<FieldDecl>(IFD->chain().front())) { |
| 1441 | + return findStyleKindForField(F, Type, NamingStyles); |
| 1442 | + } |
| 1443 | + |
| 1444 | + if (const auto *V = IFD->getVarDecl()) { |
| 1445 | + return findStyleKindForVar(V, Type, NamingStyles); |
| 1446 | + } |
| 1447 | + |
| 1448 | + return SK_Invalid; |
| 1449 | +} |
| 1450 | + |
| 1451 | +StyleKind IdentifierNamingCheck::findStyleKindForField( |
| 1452 | + const FieldDecl *Field, QualType Type, |
| 1453 | + ArrayRef<std::optional<NamingStyle>> NamingStyles) const { |
| 1454 | + if (!Type.isNull() && Type.isConstQualified()) { |
| 1455 | + if (NamingStyles[SK_ConstantMember]) |
| 1456 | + return SK_ConstantMember; |
| 1457 | + |
| 1458 | + if (NamingStyles[SK_Constant]) |
| 1459 | + return SK_Constant; |
| 1460 | + } |
| 1461 | + |
| 1462 | + if (Field->getAccess() == AS_private && NamingStyles[SK_PrivateMember]) |
| 1463 | + return SK_PrivateMember; |
| 1464 | + |
| 1465 | + if (Field->getAccess() == AS_protected && NamingStyles[SK_ProtectedMember]) |
| 1466 | + return SK_ProtectedMember; |
| 1467 | + |
| 1468 | + if (Field->getAccess() == AS_public && NamingStyles[SK_PublicMember]) |
| 1469 | + return SK_PublicMember; |
| 1470 | + |
| 1471 | + if (NamingStyles[SK_Member]) |
| 1472 | + return SK_Member; |
| 1473 | + |
| 1474 | + return SK_Invalid; |
| 1475 | +} |
| 1476 | + |
| 1477 | +StyleKind IdentifierNamingCheck::findStyleKindForVar( |
| 1478 | + const VarDecl *Var, QualType Type, |
| 1479 | + ArrayRef<std::optional<NamingStyle>> NamingStyles) const { |
| 1480 | + if (Var->isConstexpr() && NamingStyles[SK_ConstexprVariable]) |
| 1481 | + return SK_ConstexprVariable; |
| 1482 | + |
| 1483 | + if (!Type.isNull() && Type.isConstQualified()) { |
| 1484 | + if (Var->isStaticDataMember() && NamingStyles[SK_ClassConstant]) |
| 1485 | + return SK_ClassConstant; |
| 1486 | + |
| 1487 | + if (Var->isFileVarDecl() && Type.getTypePtr()->isAnyPointerType() && |
| 1488 | + NamingStyles[SK_GlobalConstantPointer]) |
| 1489 | + return SK_GlobalConstantPointer; |
| 1490 | + |
| 1491 | + if (Var->isFileVarDecl() && NamingStyles[SK_GlobalConstant]) |
| 1492 | + return SK_GlobalConstant; |
| 1493 | + |
| 1494 | + if (Var->isStaticLocal() && NamingStyles[SK_StaticConstant]) |
| 1495 | + return SK_StaticConstant; |
| 1496 | + |
| 1497 | + if (Var->isLocalVarDecl() && Type.getTypePtr()->isAnyPointerType() && |
| 1498 | + NamingStyles[SK_LocalConstantPointer]) |
| 1499 | + return SK_LocalConstantPointer; |
| 1500 | + |
| 1501 | + if (Var->isLocalVarDecl() && NamingStyles[SK_LocalConstant]) |
| 1502 | + return SK_LocalConstant; |
| 1503 | + |
| 1504 | + if (Var->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalConstant]) |
| 1505 | + return SK_LocalConstant; |
| 1506 | + |
| 1507 | + if (NamingStyles[SK_Constant]) |
| 1508 | + return SK_Constant; |
| 1509 | + } |
| 1510 | + |
| 1511 | + if (Var->isStaticDataMember() && NamingStyles[SK_ClassMember]) |
| 1512 | + return SK_ClassMember; |
| 1513 | + |
| 1514 | + if (Var->isFileVarDecl() && Type.getTypePtr()->isAnyPointerType() && |
| 1515 | + NamingStyles[SK_GlobalPointer]) |
| 1516 | + return SK_GlobalPointer; |
| 1517 | + |
| 1518 | + if (Var->isFileVarDecl() && NamingStyles[SK_GlobalVariable]) |
| 1519 | + return SK_GlobalVariable; |
| 1520 | + |
| 1521 | + if (Var->isStaticLocal() && NamingStyles[SK_StaticVariable]) |
| 1522 | + return SK_StaticVariable; |
| 1523 | + |
| 1524 | + if (Var->isLocalVarDecl() && Type.getTypePtr()->isAnyPointerType() && |
| 1525 | + NamingStyles[SK_LocalPointer]) |
| 1526 | + return SK_LocalPointer; |
| 1527 | + |
| 1528 | + if (Var->isLocalVarDecl() && NamingStyles[SK_LocalVariable]) |
| 1529 | + return SK_LocalVariable; |
| 1530 | + |
| 1531 | + if (Var->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalVariable]) |
| 1532 | + return SK_LocalVariable; |
| 1533 | + |
| 1534 | + if (NamingStyles[SK_Variable]) |
| 1535 | + return SK_Variable; |
| 1536 | + |
| 1537 | + return SK_Invalid; |
| 1538 | +} |
| 1539 | + |
1499 | 1540 | } // namespace readability
|
1500 | 1541 | } // namespace clang::tidy
|
0 commit comments