[PATCH] D49766: Fix a crash when an error occurs in Template and the initializer is a nullptr for C++17

Balaji Iyer via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 24 16:10:27 PDT 2018


bviyer created this revision.
bviyer added reviewers: arphaman, erik.pilkington, dexonsmith.
Herald added a subscriber: cfe-commits.

When the error has occurred for one of the variables inside a template, the Loc function will try to find the end-location of the final item. If this variable is default initialized to nullptr, then the templateArgumentLoc function is invoked. It has an assert to see if the Argument's kind is Expression. If the default initializer is a nullptr, then the argument's kind will be NullPtr, not Expression. This will cause a compiler crash. This patch will allow NullPtr as a possibility for expression's kind.


Repository:
  rC Clang

https://reviews.llvm.org/D49766

Files:
  include/clang/AST/TemplateBase.h
  test/SemaObjCXX/class-templ-error-null-init.mm


Index: test/SemaObjCXX/class-templ-error-null-init.mm
===================================================================
--- /dev/null
+++ test/SemaObjCXX/class-templ-error-null-init.mm
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c++17  -verify %s
+
+template <typename a, typename b, d * = nullptr> // expected-error {{unknown type name 'd'}}
+class e {
+  public:
+    e(a,b) {}
+};
+      e c([]{} ,[]{} );
Index: include/clang/AST/TemplateBase.h
===================================================================
--- include/clang/AST/TemplateBase.h
+++ include/clang/AST/TemplateBase.h
@@ -465,7 +465,14 @@
 
   TemplateArgumentLoc(const TemplateArgument &Argument, Expr *E)
       : Argument(Argument), LocInfo(E) {
-    assert(Argument.getKind() == TemplateArgument::Expression);
+
+    /* When an error occurs in a template value that is defaulted to
+       nullptr then the nullptr is left as-is and this function will
+       take in an argument that is of type NullPtr when compiled using
+       C++17 standard.  This is to make sure the compiler does not crash
+       and proceeds through.  */
+    assert(Argument.getKind() == TemplateArgument::NullPtr ||
+           Argument.getKind() == TemplateArgument::Expression);
   }
 
   TemplateArgumentLoc(const TemplateArgument &Argument, 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D49766.157156.patch
Type: text/x-patch
Size: 1315 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180724/ac27baf5/attachment.bin>


More information about the cfe-commits mailing list