hokein updated this revision to Diff 253809. hokein marked an inline comment as done. hokein added a comment.
- mark CtorDecl as invalid when the Initializer init expr contains errors - add a testcase that would crash the previous version of the patch Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D77041/new/ https://reviews.llvm.org/D77041 Files: clang/lib/Parse/ParseDeclCXX.cpp clang/lib/Sema/SemaDecl.cpp clang/lib/Sema/SemaDeclCXX.cpp clang/test/SemaCXX/invalid-constructor-init.cpp Index: clang/test/SemaCXX/invalid-constructor-init.cpp =================================================================== --- /dev/null +++ clang/test/SemaCXX/invalid-constructor-init.cpp @@ -0,0 +1,23 @@ +// RUN: %clang_cc1 -frecovery-ast -verify %s + +struct X {// expected-note 2{{candidate constructor }} + int Y; + constexpr X() : Y(foo()) {} // expected-error {{use of undeclared identifier 'foo'}} +}; +// no crash on evaluating the constexpr ctor. +// FIXME: get rid of the bogus diagnostic below. +constexpr int Z = X().Y; // expected-error {{no matching constructor for initialization of 'X'}} + +struct X2 { + int Y = foo(); // expected-error {{use of undeclared identifier 'foo'}} \ + // expected-note {{subexpression not valid in a constant expression}} + constexpr X2() {} // expected-error {{constexpr constructor never produces a constant expression}} +}; + +struct CycleDelegate { + int Y; + CycleDelegate(int) : Y(foo()) {} // expected-error {{use of undeclared identifier 'foo'}} + // FIXME: get rid of the bogus "delegation cycle" diagnostic + // CycleDeclegate(int) is marked as invalid. + CycleDelegate(float) : CycleDelegate(1) {} // expected-error {{creates a delegation cycle}} +}; Index: clang/lib/Sema/SemaDeclCXX.cpp =================================================================== --- clang/lib/Sema/SemaDeclCXX.cpp +++ clang/lib/Sema/SemaDeclCXX.cpp @@ -1685,6 +1685,7 @@ // This implements C++11 [dcl.constexpr]p3,4, as amended by DR1360. bool Sema::CheckConstexprFunctionDefinition(const FunctionDecl *NewFD, CheckConstexprKind Kind) { + assert(!NewFD->isInvalidDecl()); const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); if (MD && MD->isInstance()) { // C++11 [dcl.constexpr]p4: @@ -5000,7 +5001,8 @@ for (unsigned i = 0; i < Initializers.size(); i++) { CXXCtorInitializer *Member = Initializers[i]; - + if (Member->getInit() && Member->getInit()->containsErrors()) + Constructor->setInvalidDecl(); if (Member->isBaseInitializer()) Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; else { Index: clang/lib/Sema/SemaDecl.cpp =================================================================== --- clang/lib/Sema/SemaDecl.cpp +++ clang/lib/Sema/SemaDecl.cpp @@ -45,6 +45,7 @@ #include "clang/Sema/Template.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/Triple.h" +#include "llvm/Support/Casting.h" #include <algorithm> #include <cstring> #include <functional> Index: clang/lib/Parse/ParseDeclCXX.cpp =================================================================== --- clang/lib/Parse/ParseDeclCXX.cpp +++ clang/lib/Parse/ParseDeclCXX.cpp @@ -3454,9 +3454,12 @@ } MemInitResult MemInit = ParseMemInitializer(ConstructorDecl); - if (!MemInit.isInvalid()) + if (!MemInit.isInvalid()) { MemInitializers.push_back(MemInit.get()); - else + if (MemInit.get()->getInit() && + MemInit.get()->getInit()->containsErrors()) + AnyErrors = true; + } else AnyErrors = true; if (Tok.is(tok::comma))
Index: clang/test/SemaCXX/invalid-constructor-init.cpp =================================================================== --- /dev/null +++ clang/test/SemaCXX/invalid-constructor-init.cpp @@ -0,0 +1,23 @@ +// RUN: %clang_cc1 -frecovery-ast -verify %s + +struct X {// expected-note 2{{candidate constructor }} + int Y; + constexpr X() : Y(foo()) {} // expected-error {{use of undeclared identifier 'foo'}} +}; +// no crash on evaluating the constexpr ctor. +// FIXME: get rid of the bogus diagnostic below. +constexpr int Z = X().Y; // expected-error {{no matching constructor for initialization of 'X'}} + +struct X2 { + int Y = foo(); // expected-error {{use of undeclared identifier 'foo'}} \ + // expected-note {{subexpression not valid in a constant expression}} + constexpr X2() {} // expected-error {{constexpr constructor never produces a constant expression}} +}; + +struct CycleDelegate { + int Y; + CycleDelegate(int) : Y(foo()) {} // expected-error {{use of undeclared identifier 'foo'}} + // FIXME: get rid of the bogus "delegation cycle" diagnostic + // CycleDeclegate(int) is marked as invalid. + CycleDelegate(float) : CycleDelegate(1) {} // expected-error {{creates a delegation cycle}} +}; Index: clang/lib/Sema/SemaDeclCXX.cpp =================================================================== --- clang/lib/Sema/SemaDeclCXX.cpp +++ clang/lib/Sema/SemaDeclCXX.cpp @@ -1685,6 +1685,7 @@ // This implements C++11 [dcl.constexpr]p3,4, as amended by DR1360. bool Sema::CheckConstexprFunctionDefinition(const FunctionDecl *NewFD, CheckConstexprKind Kind) { + assert(!NewFD->isInvalidDecl()); const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); if (MD && MD->isInstance()) { // C++11 [dcl.constexpr]p4: @@ -5000,7 +5001,8 @@ for (unsigned i = 0; i < Initializers.size(); i++) { CXXCtorInitializer *Member = Initializers[i]; - + if (Member->getInit() && Member->getInit()->containsErrors()) + Constructor->setInvalidDecl(); if (Member->isBaseInitializer()) Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; else { Index: clang/lib/Sema/SemaDecl.cpp =================================================================== --- clang/lib/Sema/SemaDecl.cpp +++ clang/lib/Sema/SemaDecl.cpp @@ -45,6 +45,7 @@ #include "clang/Sema/Template.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/Triple.h" +#include "llvm/Support/Casting.h" #include <algorithm> #include <cstring> #include <functional> Index: clang/lib/Parse/ParseDeclCXX.cpp =================================================================== --- clang/lib/Parse/ParseDeclCXX.cpp +++ clang/lib/Parse/ParseDeclCXX.cpp @@ -3454,9 +3454,12 @@ } MemInitResult MemInit = ParseMemInitializer(ConstructorDecl); - if (!MemInit.isInvalid()) + if (!MemInit.isInvalid()) { MemInitializers.push_back(MemInit.get()); - else + if (MemInit.get()->getInit() && + MemInit.get()->getInit()->containsErrors()) + AnyErrors = true; + } else AnyErrors = true; if (Tok.is(tok::comma))
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits