[PATCH] D131662: [clang] Try to improve diagnostics about uninitialized constexpr variables

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 11 02:35:59 PDT 2022


tbaeder created this revision.
tbaeder added a reviewer: aaron.ballman.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Consider:

  constexpr int a;

the error message for this is currently:

  error: default initialization of an object of const type 'const int'
  constexpr int a;
                ^
                  = 0

which makes very little sense to me. With this patch, the output is instead:

  error: constexpr variable 'a' must be initialized by a constant expression
  constexpr int a;
                ^
                  = 0

which is much better. Tells the user exactly what is missing.

For

  constexpr int a[];

before this patch, the output is:

  error: definition of variable with array type needs an explicit size or an initializer
  constexpr int a[];
                ^

The error message is not even true in this case, since _only_ passing a size won't work. It must be initialized by a constant expression, so now the error message is:

  error: constexpr variable 'a' must be initialized by a constant expression
  constexpr int a[];
                ^

and a fixit hint if a size was provided:

  error: constexpr variable 'a' must be initialized by a constant expression
  constexpr int a[2];
                ^
                     = {}

(not sure about that part)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131662

Files:
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaFixItUtils.cpp
  clang/lib/Sema/SemaInit.cpp
  clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p1.cpp
  clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p9.cpp
  clang/test/SemaCXX/constant-expression-cxx11.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131662.451780.patch
Type: text/x-patch
Size: 5716 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220811/e0d184c8/attachment.bin>


More information about the cfe-commits mailing list