]> granicus.if.org Git - clang/commitdiff
Consume unexpected "template" keywords after "using"
authorRichard Trieu <rtrieu@google.com>
Wed, 1 May 2019 23:33:49 +0000 (23:33 +0000)
committerRichard Trieu <rtrieu@google.com>
Wed, 1 May 2019 23:33:49 +0000 (23:33 +0000)
The parser was dealing with unexpected "template" keywords after "using"
keywords too late and putting the parser into the wrong state, which could
lead to a crash down the line.  This change allows the parser to consume the
bad "template" keywords earlier, and continue parsing as if "template" was
never there to begin with for better error recovery.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@359740 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticParseKinds.td
lib/Parse/ParseDeclCXX.cpp
test/Parser/using-template.cpp [new file with mode: 0644]

index 3003d0a8d339657c914e8b027e494cd034ecfbd6..4bd29b629de4750c2384a14476b4c25f9a7cb8f0 100644 (file)
@@ -683,6 +683,8 @@ def err_id_after_template_in_nested_name_spec : Error<
   "expected template name after 'template' keyword in nested name specifier">;
 def err_unexpected_template_in_unqualified_id : Error<
   "'template' keyword not permitted here">;
+def err_unexpected_template_after_using : Error<
+  "'template' keyword not permitted after 'using' keyword">;
 def err_two_right_angle_brackets_need_space : Error<
   "a space is required between consecutive right angle brackets (use '> >')">;
 def err_right_angle_bracket_equal_needs_space : Error<
index 7d2fcbd3e1b1b2bf4492c62ba0592cb631630c07..c4fe23c60c304dc50b96225c7fba71b779c33647 100644 (file)
@@ -474,6 +474,13 @@ Parser::ParseUsingDirectiveOrDeclaration(DeclaratorContext Context,
     return nullptr;
   }
 
+  // Consume unexpected 'template' keywords.
+  while (Tok.is(tok::kw_template)) {
+    SourceLocation TemplateLoc = ConsumeToken();
+    Diag(TemplateLoc, diag::err_unexpected_template_after_using)
+        << FixItHint::CreateRemoval(TemplateLoc);
+  }
+
   // 'using namespace' means this is a using-directive.
   if (Tok.is(tok::kw_namespace)) {
     // Template parameters are always an error here.
@@ -2542,6 +2549,13 @@ Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
     // Eat 'using'.
     SourceLocation UsingLoc = ConsumeToken();
 
+    // Consume unexpected 'template' keywords.
+    while (Tok.is(tok::kw_template)) {
+      SourceLocation TemplateLoc = ConsumeToken();
+      Diag(TemplateLoc, diag::err_unexpected_template_after_using)
+          << FixItHint::CreateRemoval(TemplateLoc);
+    }
+
     if (Tok.is(tok::kw_namespace)) {
       Diag(UsingLoc, diag::err_using_namespace_in_class);
       SkipUntil(tok::semi, StopBeforeMatch);
diff --git a/test/Parser/using-template.cpp b/test/Parser/using-template.cpp
new file mode 100644 (file)
index 0000000..686873d
--- /dev/null
@@ -0,0 +1,52 @@
+// RUN: %clang_cc1 %s -verify
+
+namespace N1 {
+template <typename... Ts>
+struct Foo {
+  template <typename T>
+  struct Bar {
+    static constexpr bool is_present = false;
+  };
+};
+
+template <typename T, typename... Ts>
+struct Foo<T, Ts...> : public Foo<Ts...> {
+  using template Foo<Ts...>::Bar;
+  // expected-error@-1 {{'template' keyword not permitted after 'using' keyword}}
+};
+}
+
+namespace N2 {
+namespace foo {
+  using I = int;
+}
+using template namespace foo;
+// expected-error@-1 {{'template' keyword not permitted after 'using' keyword}}
+using template template namespace foo;
+// expected-error@-1 2{{'template' keyword not permitted after 'using' keyword}}
+I i;
+}
+
+namespace N3 {
+namespace foo {
+  using I = int;
+}
+using template foo::I;
+// expected-error@-1 {{'template' keyword not permitted after 'using' keyword}}
+I i;
+}
+
+namespace N4 {
+template <typename T>
+class A {};
+
+template <typename T>
+using B = A<T>;
+B<int> b;
+
+using template <typename T> C = A<T>;
+// expected-error@-1 {{'template' keyword not permitted after 'using' keyword}}
+// expected-error@-2 {{expected unqualified-id}}
+C<int> c;
+// expected-error@-1 {{no template named 'C'}}
+}