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

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 17 12:44:53 PDT 2022


aaron.ballman added a comment.

Thanks for working on this, I like the direction it's heading! One question I'm kicking around my head is, should we do something similar for `const` variables?

  const int i; // error: default initialization of an object of const type 'const int'

it sure seems like this could be similarly improved to say something along the lines of `error: variable 'i' of const type must be initialized`

Also, don't forget to add a release note.



================
Comment at: clang/lib/Sema/SemaFixItUtils.cpp:208-210
+  if (T->isArrayType()) {
+    return " = {}";
+  }
----------------
I don't think this is a good change, consider:
```
int array[] = {};
```
zero-sized arrays are an extension in both C and C++, and the empty initializer is a GNU extension in C (at least until C2x).


================
Comment at: clang/lib/Sema/SemaInit.cpp:8063
     // handled in the Failed() branch above.
-    QualType DestType = Entity.getType();
-    S.Diag(Kind.getLocation(), DiagID)
-        << DestType << (bool)DestType->getAs<RecordType>()
-        << FixItHint::CreateInsertion(ZeroInitializationFixitLoc,
-                                      ZeroInitializationFixit);
+    if (!DestType->getAs<RecordType>() && VD && VD->isConstexpr()) {
+      // Use a more useful diagnostic for constexpr variables.
----------------
Why the check for a record type?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D131662/new/

https://reviews.llvm.org/D131662



More information about the cfe-commits mailing list