Skip to content

Commit

Permalink
[c++2a] Treat 'concept' and 'requires' as keywords, add compat warnin…
Browse files Browse the repository at this point in the history
…g for C++17 and before.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@310803 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
zygoloid committed Aug 13, 2017
1 parent 4fe40d8 commit d1cebef
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
2 changes: 2 additions & 0 deletions include/clang/Basic/DiagnosticGroups.td
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ def CXX1zCompat : DiagGroup<"c++1z-compat", [DeprecatedRegister,
DeprecatedIncrementBool,
CXX1zCompatMangling]>;

def CXX2aCompat : DiagGroup<"c++2a-compat">;

def ExitTimeDestructors : DiagGroup<"exit-time-destructors">;
def FlexibleArrayExtensions : DiagGroup<"flexible-array-extensions">;
def FourByteMultiChar : DiagGroup<"four-char-constants">;
Expand Down
2 changes: 2 additions & 0 deletions include/clang/Basic/DiagnosticLexKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def ext_token_used : Extension<"extension used">,

def warn_cxx11_keyword : Warning<"'%0' is a keyword in C++11">,
InGroup<CXX11Compat>, DefaultIgnore;
def warn_cxx2a_keyword : Warning<"'%0' is a keyword in C++2a">,
InGroup<CXX2aCompat>, DefaultIgnore;

def ext_unterminated_char_or_string : ExtWarn<
"missing terminating %select{'|'\"'}0 character">, InGroup<InvalidPPToken>;
Expand Down
9 changes: 7 additions & 2 deletions include/clang/Basic/TokenKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
#ifndef CXX11_KEYWORD
#define CXX11_KEYWORD(X,Y) KEYWORD(X,KEYCXX11|(Y))
#endif
#ifndef CXX2A_KEYWORD
#define CXX2A_KEYWORD(X,Y) KEYWORD(X,KEYCXX2A|(Y))
#endif
#ifndef CONCEPTS_KEYWORD
#define CONCEPTS_KEYWORD(X) KEYWORD(X,KEYCONCEPTS)
#define CONCEPTS_KEYWORD(X) CXX2A_KEYWORD(X,KEYCONCEPTS)
#endif
#ifndef MODULES_KEYWORD
#define MODULES_KEYWORD(X) KEYWORD(X,KEYMODULES)
Expand Down Expand Up @@ -236,6 +239,7 @@ PUNCTUATOR(caretcaret, "^^")
// implementation namespace
// KEYNOCXX - This is a keyword in every non-C++ dialect.
// KEYCXX11 - This is a C++ keyword introduced to C++ in C++11
// KEYCXX2A - This is a C++ keyword introduced to C++ in C++2a
// KEYCONCEPTS - This is a keyword if the C++ extensions for concepts
// are enabled.
// KEYMODULES - This is a keyword if the C++ extensions for modules
Expand Down Expand Up @@ -362,7 +366,7 @@ CXX11_KEYWORD(nullptr , 0)
CXX11_KEYWORD(static_assert , 0)
CXX11_KEYWORD(thread_local , 0)

// C++ concepts TS keywords
// C++2a / concepts TS keywords
CONCEPTS_KEYWORD(concept)
CONCEPTS_KEYWORD(requires)

Expand Down Expand Up @@ -810,6 +814,7 @@ ANNOTATION(module_end)
#undef TYPE_TRAIT_1
#undef TYPE_TRAIT
#undef CONCEPTS_KEYWORD
#undef CXX2A_KEYWORD
#undef CXX11_KEYWORD
#undef KEYWORD
#undef PUNCTUATOR
Expand Down
8 changes: 6 additions & 2 deletions lib/Basic/IdentifierTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ namespace {
KEYZVECTOR = 0x40000,
KEYCOROUTINES = 0x80000,
KEYMODULES = 0x100000,
KEYALL = (0x1fffff & ~KEYNOMS18 &
KEYCXX2A = 0x200000,
KEYALLCXX = KEYCXX | KEYCXX11 | KEYCXX2A,
KEYALL = (0x3fffff & ~KEYNOMS18 &
~KEYNOOPENCL) // KEYNOMS18 and KEYNOOPENCL are used to exclude.
};

Expand All @@ -134,6 +136,7 @@ static KeywordStatus getKeywordStatus(const LangOptions &LangOpts,
if (Flags == KEYALL) return KS_Enabled;
if (LangOpts.CPlusPlus && (Flags & KEYCXX)) return KS_Enabled;
if (LangOpts.CPlusPlus11 && (Flags & KEYCXX11)) return KS_Enabled;
if (LangOpts.CPlusPlus2a && (Flags & KEYCXX2A)) return KS_Enabled;
if (LangOpts.C99 && (Flags & KEYC99)) return KS_Enabled;
if (LangOpts.GNUKeywords && (Flags & KEYGNU)) return KS_Extension;
if (LangOpts.MicrosoftExt && (Flags & KEYMS)) return KS_Extension;
Expand All @@ -152,7 +155,7 @@ static KeywordStatus getKeywordStatus(const LangOptions &LangOpts,
if (LangOpts.ConceptsTS && (Flags & KEYCONCEPTS)) return KS_Enabled;
if (LangOpts.CoroutinesTS && (Flags & KEYCOROUTINES)) return KS_Enabled;
if (LangOpts.ModulesTS && (Flags & KEYMODULES)) return KS_Enabled;
if (LangOpts.CPlusPlus && (Flags & KEYCXX11)) return KS_Future;
if (LangOpts.CPlusPlus && (Flags & KEYALLCXX)) return KS_Future;
return KS_Disabled;
}

Expand Down Expand Up @@ -264,6 +267,7 @@ bool IdentifierInfo::isCPlusPlusKeyword(const LangOptions &LangOpts) const {
LangOptions LangOptsNoCPP = LangOpts;
LangOptsNoCPP.CPlusPlus = false;
LangOptsNoCPP.CPlusPlus11 = false;
LangOptsNoCPP.CPlusPlus2a = false;
return !isKeyword(LangOptsNoCPP);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/Lex/Preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,8 @@ static diag::kind getFutureCompatDiagKind(const IdentifierInfo &II,
return llvm::StringSwitch<diag::kind>(II.getName())
#define CXX11_KEYWORD(NAME, FLAGS) \
.Case(#NAME, diag::warn_cxx11_keyword)
#define CXX2A_KEYWORD(NAME, FLAGS) \
.Case(#NAME, diag::warn_cxx2a_keyword)
#include "clang/Basic/TokenKinds.def"
;

Expand Down
3 changes: 2 additions & 1 deletion test/Lexer/keywords_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// RUN: %clang_cc1 -std=c++03 -fsyntax-only %s
// RUN: %clang_cc1 -std=c++11 -DCXX11 -fsyntax-only %s
// RUN: %clang_cc1 -std=c++14 -fconcepts-ts -DCXX11 -DCONCEPTS -fsyntax-only %s
// RUN: %clang_cc1 -std=c++2a -DCXX11 -DCXX2A -fsyntax-only %s
// RUN: %clang_cc1 -std=c++03 -fdeclspec -DDECLSPEC -fsyntax-only %s
// RUN: %clang_cc1 -std=c++03 -fms-extensions -DDECLSPEC -fsyntax-only %s
// RUN: %clang_cc1 -std=c++03 -fborland-extensions -DDECLSPEC -fsyntax-only %s
Expand All @@ -19,7 +20,7 @@
#define NOT_KEYWORD(NAME) _Static_assert(__is_identifier(NAME), #NAME)
#define IS_TYPE(NAME) void is_##NAME##_type() { int f(NAME); }

#ifdef CONCEPTS
#if defined(CONCEPTS) || defined(CXX2A)
#define CONCEPTS_KEYWORD(NAME) IS_KEYWORD(NAME)
#else
#define CONCEPTS_KEYWORD(NAME) NOT_KEYWORD(NAME)
Expand Down

0 comments on commit d1cebef

Please sign in to comment.