diff options
author | Po-yao Chang <poyaoc97@gmail.com> | 2023-07-13 23:42:51 +0800 |
---|---|---|
committer | Po-yao Chang <poyaoc97@gmail.com> | 2023-07-19 07:23:34 +0800 |
commit | 5ce5e983f82c802e44faa8ed42d605d70c045ba9 (patch) | |
tree | 9f23ade1a166729184db4ab6bde61f36eac1a48c /clang/lib | |
parent | ab9b3c84a588f86e7f66eeb577bea7155817ff06 (diff) | |
download | llvm-5ce5e983f82c802e44faa8ed42d605d70c045ba9.zip llvm-5ce5e983f82c802e44faa8ed42d605d70c045ba9.tar.gz llvm-5ce5e983f82c802e44faa8ed42d605d70c045ba9.tar.bz2 |
[Clang] Add warnings for CWG2521
1. Teach -Wuser-defined-literals to warn on using double underscores in
literal suffix identifiers.
2. Add -Wdeprecated-literal-operator to warn about the use of the first
grammar production of literal-operator-id, which defaults to off for now.
Differential Revision: https://reviews.llvm.org/D152632
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Basic/IdentifierTable.cpp | 13 | ||||
-rw-r--r-- | clang/lib/Sema/SemaDeclCXX.cpp | 17 | ||||
-rw-r--r-- | clang/lib/Sema/SemaExprCXX.cpp | 17 |
3 files changed, 33 insertions, 14 deletions
diff --git a/clang/lib/Basic/IdentifierTable.cpp b/clang/lib/Basic/IdentifierTable.cpp index 661e9f7..0065a61 100644 --- a/clang/lib/Basic/IdentifierTable.cpp +++ b/clang/lib/Basic/IdentifierTable.cpp @@ -397,6 +397,19 @@ IdentifierInfo::isReserved(const LangOptions &LangOpts) const { return ReservedIdentifierStatus::NotReserved; } +ReservedLiteralSuffixIdStatus +IdentifierInfo::isReservedLiteralSuffixId() const { + StringRef Name = getName(); + + if (Name[0] != '_') + return ReservedLiteralSuffixIdStatus::NotStartsWithUnderscore; + + if (Name.contains("__")) + return ReservedLiteralSuffixIdStatus::ContainsDoubleUnderscore; + + return ReservedLiteralSuffixIdStatus::NotReserved; +} + StringRef IdentifierInfo::deuglifiedName() const { StringRef Name = getName(); if (Name.size() >= 2 && Name.front() == '_' && diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 5c68642..4848af9 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -16447,15 +16447,18 @@ bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { } } - StringRef LiteralName - = FnDecl->getDeclName().getCXXLiteralIdentifier()->getName(); - if (LiteralName[0] != '_' && + const IdentifierInfo *II = FnDecl->getDeclName().getCXXLiteralIdentifier(); + ReservedLiteralSuffixIdStatus Status = II->isReservedLiteralSuffixId(); + if (Status != ReservedLiteralSuffixIdStatus::NotReserved && !getSourceManager().isInSystemHeader(FnDecl->getLocation())) { - // C++11 [usrlit.suffix]p1: - // Literal suffix identifiers that do not start with an underscore - // are reserved for future standardization. + // C++23 [usrlit.suffix]p1: + // Literal suffix identifiers that do not start with an underscore are + // reserved for future standardization. Literal suffix identifiers that + // contain a double underscore __ are reserved for use by C++ + // implementations. Diag(FnDecl->getLocation(), diag::warn_user_literal_reserved) - << StringLiteralParser::isValidUDSuffix(getLangOpts(), LiteralName); + << static_cast<int>(Status) + << StringLiteralParser::isValidUDSuffix(getLangOpts(), II->getName()); } return false; diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index 168a0f3..3e43b69 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -502,13 +502,16 @@ bool Sema::checkLiteralOperatorId(const CXXScopeSpec &SS, IdentifierInfo *II = Name.Identifier; ReservedIdentifierStatus Status = II->isReserved(PP.getLangOpts()); SourceLocation Loc = Name.getEndLoc(); - if (isReservedInAllContexts(Status) && - !PP.getSourceManager().isInSystemHeader(Loc)) { - Diag(Loc, diag::warn_reserved_extern_symbol) - << II << static_cast<int>(Status) - << FixItHint::CreateReplacement( - Name.getSourceRange(), - (StringRef("operator\"\"") + II->getName()).str()); + if (!PP.getSourceManager().isInSystemHeader(Loc)) { + if (auto Hint = FixItHint::CreateReplacement( + Name.getSourceRange(), + (StringRef("operator\"\"") + II->getName()).str()); + isReservedInAllContexts(Status)) { + Diag(Loc, diag::warn_reserved_extern_symbol) + << II << static_cast<int>(Status) << Hint; + } else { + Diag(Loc, diag::warn_deprecated_literal_operator_id) << II << Hint; + } } } |