Author: Shafik Yaghmour
Date: 2022-08-26T09:40:43-07:00
New Revision: 21dfe482e13e3b64801372f8b3db75eeb85b3135

URL: 
https://github.com/llvm/llvm-project/commit/21dfe482e13e3b64801372f8b3db75eeb85b3135
DIFF: 
https://github.com/llvm/llvm-project/commit/21dfe482e13e3b64801372f8b3db75eeb85b3135.diff

LOG: [Clang] Fix assert in Sema::LookupTemplateName so that it does not attempt 
an unconditional cast to TagType

In Sema::LookupTemplateName(...) seeks to assert that the ObjectType is complete
or being defined. If the type is incomplete it will attempt to unconditionally
cast it to a TagType and not all incomplete types are a TagType. For example the
type could be void or it could be an IncompleteArray.

This change adds an additional check to confirm it is a TagType before 
attempting
to check if it is incomplete or being defined

Differential Revision: https://reviews.llvm.org/D132712

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaTemplate.cpp
    clang/test/SemaCXX/member-expr.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b6d1dbedb2c59..b19a81e848387 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -81,6 +81,9 @@ Bug Fixes
 - Fix a crash when generating code coverage information for an
   ``if consteval`` statement. This fixes
   `Issue 57377 <https://github.com/llvm/llvm-project/issues/57377>`_.
+- Fix assert that triggers a crash during template name lookup when a type was
+  incomplete but was not also a TagType. This fixes
+  `Issue 57387 <https://github.com/llvm/llvm-project/issues/57387>`_.
 
 
 Improvements to Clang's diagnostics

diff  --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 1d9c364771927..b06612d02797b 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -399,6 +399,7 @@ bool Sema::LookupTemplateName(LookupResult &Found,
     LookupCtx = computeDeclContext(ObjectType);
     IsDependent = !LookupCtx && ObjectType->isDependentType();
     assert((IsDependent || !ObjectType->isIncompleteType() ||
+            !ObjectType->getAs<TagType>() ||
             ObjectType->castAs<TagType>()->isBeingDefined()) &&
            "Caller should have completed object type");
 

diff  --git a/clang/test/SemaCXX/member-expr.cpp 
b/clang/test/SemaCXX/member-expr.cpp
index 3497ef596480e..75c9ef0caa2e0 100644
--- a/clang/test/SemaCXX/member-expr.cpp
+++ b/clang/test/SemaCXX/member-expr.cpp
@@ -228,3 +228,19 @@ namespace pr16676 {
         .i;  // expected-error {{member reference type 'S *' is a pointer; did 
you mean to use '->'}}
   }
 }
+
+namespace LookupTemplateNameAssert {
+void f() {}
+
+typedef int at[];
+const at& f2(){}
+
+void g() {
+  f().junk<int>(); // expected-error {{member reference base type 'void' is 
not a structure or union}}
+// expected-error@-1 {{expected '(' for function-style cast or type 
construction}}
+// expected-error@-2 {{expected expression}}
+  f2().junk<int>(); // expected-error {{member reference base type 'const at' 
(aka 'const int[]') is not a structure or union}}
+// expected-error@-1 {{expected '(' for function-style cast or type 
construction}}
+// expected-error@-2 {{expected expression}}
+}
+}


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to