[clang] 355f1c7 - [Clang] Fix PR28101

2022-03-23 Thread PoYao Chang via cfe-commits

Author: PoYao Chang
Date: 2022-03-24T00:38:45+08:00
New Revision: 355f1c75aa66fc3d9bef897375f5e0979a55001d

URL: 
https://github.com/llvm/llvm-project/commit/355f1c75aa66fc3d9bef897375f5e0979a55001d
DIFF: 
https://github.com/llvm/llvm-project/commit/355f1c75aa66fc3d9bef897375f5e0979a55001d.diff

LOG: [Clang] Fix PR28101

Fixes https://github.com/llvm/llvm-project/issues/28475 (PR28101)
by setting identifier for invalid member variables with template parameters,
so that the invalid declarators would not crash clang.

See also: https://github.com/llvm/llvm-project/commit/942c03910a

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

Added: 
clang/test/SemaCXX/PR28101.cpp

Modified: 
clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaDeclCXX.cpp

Removed: 




diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 865da1eb2aa1b..ebbed4b5ce008 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -69,6 +69,9 @@ Bug Fixes
   like ``auto&`` or ``auto**`` were added. These constraints are now checked.
   This fixes  `Issue 53911 
`_
   and  `Issue 54443 `_.
+- Previously invalid member variables with template parameters would crash 
clang.
+  Now fixed by setting identifiers for them.
+  This fixes `Issue 28475 (PR28101) 
`_.
 
 Improvements to Clang's diagnostics
 ^^^

diff  --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 008c65673ed26..7992e4a349611 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -3427,6 +3427,7 @@ Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier 
AS, Declarator &D,
   << SourceRange(D.getName().TemplateId->LAngleLoc,
  D.getName().TemplateId->RAngleLoc)
   << D.getName().TemplateId->LAngleLoc;
+  D.SetIdentifier(Name.getAsIdentifierInfo(), Loc);
 }
 
 if (SS.isSet() && !SS.isInvalid()) {

diff  --git a/clang/test/SemaCXX/PR28101.cpp b/clang/test/SemaCXX/PR28101.cpp
new file mode 100644
index 0..311c0f0db8174
--- /dev/null
+++ b/clang/test/SemaCXX/PR28101.cpp
@@ -0,0 +1,36 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s
+
+template  struct A {
+  A(void *) {}
+  T(A){}; // expected-error{{member 'A' cannot have template arguments}}\
+  // expected-error2{{member 'A' has the same name as its class}}
+};
+// Don't crash.
+A instantiate1() { return {nullptr}; } // expected-note{{in instantiation 
of template class 'A' requested here}}
+
+template  struct B {
+  B(void *) {}
+  T B{}; // expected-error{{member 'B' cannot have template arguments}}\
+  // expected-error2{{member 'B' has the same name as its class}}
+};
+// Don't crash.
+B instantiate2() { return {nullptr}; } // expected-note{{in instantiation 
of template class 'B' requested here}}
+
+template  struct S {};
+
+template  struct C {
+  C(void *) {}
+  T S{}; // expected-error{{member 'S' cannot have template arguments}}
+};
+// Don't crash.
+C instantiate3() { return {nullptr}; }
+
+template  typename U> class D {
+public:
+  D(void *) {}
+  T(D>) {} // expected-error{{member 'D' cannot have template 
arguments}}\
+  // expected-error{{expected ';' at end of declaration list}}\
+  // expected-error2{{member 'D' has the same name as its class}}
+};
+// Don't crash.
+D instantiate4() { return D(nullptr); } // expected-note{{in 
instantiation of template class 'D' requested here}}



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


[clang] 369c5fa - [NFC][Clang] Use previously declared variable instead of calling function redundantly

2022-04-11 Thread PoYao Chang via cfe-commits

Author: PoYao Chang
Date: 2022-04-12T11:10:10+08:00
New Revision: 369c5fa17be0105cbe801a78aa8419d06aad184a

URL: 
https://github.com/llvm/llvm-project/commit/369c5fa17be0105cbe801a78aa8419d06aad184a
DIFF: 
https://github.com/llvm/llvm-project/commit/369c5fa17be0105cbe801a78aa8419d06aad184a.diff

LOG: [NFC][Clang] Use previously declared variable instead of calling function 
redundantly

Added: 


Modified: 
clang/lib/Sema/SemaDeclCXX.cpp

Removed: 




diff  --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index b852b82f359d4..12db7c8595446 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -3432,7 +3432,7 @@ Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier 
AS, Declarator &D,
   << SourceRange(D.getName().TemplateId->LAngleLoc,
  D.getName().TemplateId->RAngleLoc)
   << D.getName().TemplateId->LAngleLoc;
-  D.SetIdentifier(Name.getAsIdentifierInfo(), Loc);
+  D.SetIdentifier(II, Loc);
 }
 
 if (SS.isSet() && !SS.isInvalid()) {



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


[clang] 50b1faf - [Clang] CWG 1394: Incomplete types as parameters of deleted functions

2022-04-11 Thread PoYao Chang via cfe-commits

Author: PoYao Chang
Date: 2022-04-12T11:10:10+08:00
New Revision: 50b1faf5c188956fb59ea7d9f9d470591771aedb

URL: 
https://github.com/llvm/llvm-project/commit/50b1faf5c188956fb59ea7d9f9d470591771aedb
DIFF: 
https://github.com/llvm/llvm-project/commit/50b1faf5c188956fb59ea7d9f9d470591771aedb.diff

LOG: [Clang] CWG 1394: Incomplete types as parameters of deleted functions

According to CWG 1394 and C++20 [dcl.fct.def.general]p2,
Clang should not diagnose incomplete types if function body is "= delete;".
For example:
```
struct Incomplete;
Incomplete f(Incomplete) = delete; // well-formed
```

Also close https://github.com/llvm/llvm-project/issues/52802

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

Added: 
clang/test/CXX/dcl.decl/dcl.fct.def/dcl.fct.def.general/p2.cpp

Modified: 
clang/docs/ReleaseNotes.rst
clang/include/clang/Sema/Sema.h
clang/lib/Parse/Parser.cpp
clang/lib/Sema/SemaDecl.cpp
clang/lib/Sema/SemaDeclCXX.cpp
clang/www/cxx_dr_status.html

Removed: 




diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c53fa0ad7fe01..fbc1edb8e9e2a 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -113,6 +113,10 @@ Bug Fixes
   This fixes Issue `Issue 54462 
`_.
 - Statement expressions are now disabled in default arguments in general.
   This fixes Issue `Issue 53488 
`_.
+- According to `CWG 1394 `_ and 
+  `C++20 [dcl.fct.def.general]p2 
`_,
+  Clang should not diagnose incomplete types in function definitions if the 
function body is "= delete;".
+  This fixes Issue `Issue 52802 
`_.
 
 Improvements to Clang's diagnostics
 ^^^

diff  --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index e7f417a82003b..e368726d11c66 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -2895,6 +2895,18 @@ class Sema final {
   void ActOnDocumentableDecl(Decl *D);
   void ActOnDocumentableDecls(ArrayRef Group);
 
+  enum class FnBodyKind {
+/// C++ [dcl.fct.def.general]p1
+/// function-body:
+///   ctor-initializer[opt] compound-statement
+///   function-try-block
+Other,
+///   = default ;
+Default,
+///   = delete ;
+Delete
+  };
+
   void ActOnFinishKNRParamDeclarations(Scope *S, Declarator &D,
SourceLocation LocAfterDecls);
   void CheckForFunctionRedefinition(
@@ -2902,9 +2914,12 @@ class Sema final {
   SkipBodyInfo *SkipBody = nullptr);
   Decl *ActOnStartOfFunctionDef(Scope *S, Declarator &D,
 MultiTemplateParamsArg TemplateParamLists,
-SkipBodyInfo *SkipBody = nullptr);
+SkipBodyInfo *SkipBody = nullptr,
+FnBodyKind BodyKind = FnBodyKind::Other);
   Decl *ActOnStartOfFunctionDef(Scope *S, Decl *D,
-SkipBodyInfo *SkipBody = nullptr);
+SkipBodyInfo *SkipBody = nullptr,
+FnBodyKind BodyKind = FnBodyKind::Other);
+  void SetFunctionBodyKind(Decl *D, SourceLocation Loc, FnBodyKind BodyKind);
   void ActOnStartTrailingRequiresClause(Scope *S, Declarator &D);
   ExprResult ActOnFinishTrailingRequiresClause(ExprResult ConstraintExpr);
   ExprResult ActOnRequiresClause(ExprResult ConstraintExpr);

diff  --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index 9c8892ebc4b84..f754388cdc7c8 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -1299,6 +1299,41 @@ Decl *Parser::ParseFunctionDefinition(ParsingDeclarator 
&D,
   ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |
  Scope::CompoundStmtScope);
 
+  // Parse function body eagerly if it is either '= delete;' or '= default;' as
+  // ActOnStartOfFunctionDef needs to know whether the function is deleted.
+  Sema::FnBodyKind BodyKind = Sema::FnBodyKind::Other;
+  SourceLocation KWLoc;
+  if (TryConsumeToken(tok::equal)) {
+assert(getLangOpts().CPlusPlus && "Only C++ function definitions have 
'='");
+
+if (TryConsumeToken(tok::kw_delete, KWLoc)) {
+  Diag(KWLoc, getLangOpts().CPlusPlus11
+  ? diag::warn_cxx98_compat_defaulted_deleted_function
+  : diag::ext_defaulted_deleted_function)
+  << 1 /* deleted */;
+  BodyKind = Sema::FnBodyKind::Delete;
+} else if (TryConsumeToken(tok::kw_default, KWLoc)) {
+  Diag(KWLoc, getLangOpts().CPlusPlus11
+  ? diag::warn_cxx98_compat_defaulted_deleted_functio