Просмотр исходного кода

Enable C++20 and fix infrastructure to work with it. (#3660)

C++20 mode exposes bugs in `clang-tidy` 16 that are challenging to work
around, but this should manage to do so. The patch to `bazel_clang_tidy`
has been sent upstream but no need to wait for it.

We don't actually specify a specific version of C++ in our style guide
docs, and the Google style guide has already been updated to be based on
C++20 so nothing to be done there.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
Chandler Carruth 2 лет назад
Родитель
Сommit
8bee5ebe83

+ 6 - 0
.clang-tidy

@@ -63,3 +63,9 @@ CheckOptions:
       key: readability-identifier-naming.MethodIgnoredRegexp,
       value: '^classof$',
     }
+  - {
+      # This erroneously fires in C++20 mode with LLVM 16 clang-tidy, due to:
+      # https://github.com/llvm/llvm-project/issues/46097
+      key: readability-identifier-naming.TemplateParameterIgnoredRegexp,
+      value: '^expr-type$',
+    }

+ 4 - 1
MODULE.bazel

@@ -98,7 +98,10 @@ clang_tidy_version = "d2aecc583d14c9554febeab185833c1e8cce5384"
 http_archive(
     name = "bazel_clang_tidy",
     patch_args = ["-p1"],
-    patches = ["@carbon//bazel/bazel_clang_tidy:0001_Add_hdrs.patch"],
+    patches = [
+        "@carbon//bazel/bazel_clang_tidy:0001_Add_hdrs.patch",
+        "@carbon//bazel/bazel_clang_tidy:0002-Add-a-workaround-for-a-bug-in-clang-tidy-16-and-C-20.patch",
+    ],
     sha256 = "89c198a9f544beac119bb41904d16d8870686ccb5fe946442c1576934c9e6869",
     strip_prefix = "bazel_clang_tidy-{0}".format(clang_tidy_version),
     urls = ["https://github.com/erenon/bazel_clang_tidy/archive/{0}.tar.gz".format(clang_tidy_version)],

+ 10 - 8
MODULE.bazel.lock

@@ -1,6 +1,6 @@
 {
   "lockFileVersion": 3,
-  "moduleFileHash": "a393ce0a748c6e5dba8435c276baa474703d6db8bd225f6ad3f5ce7722cd65f0",
+  "moduleFileHash": "4b00aec5b0cf13b729c8b36621c9c73398f70fe256428b409637c7c3c332b688",
   "flags": {
     "cmdRegistries": [
       "https://bcr.bazel.build/"
@@ -67,7 +67,8 @@
                   "-p1"
                 ],
                 "patches": [
-                  "@carbon//bazel/bazel_clang_tidy:0001_Add_hdrs.patch"
+                  "@carbon//bazel/bazel_clang_tidy:0001_Add_hdrs.patch",
+                  "@carbon//bazel/bazel_clang_tidy:0002-Add-a-workaround-for-a-bug-in-clang-tidy-16-and-C-20.patch"
                 ],
                 "sha256": "89c198a9f544beac119bb41904d16d8870686ccb5fe946442c1576934c9e6869",
                 "strip_prefix": "bazel_clang_tidy-d2aecc583d14c9554febeab185833c1e8cce5384",
@@ -105,7 +106,7 @@
               "devDependency": false,
               "location": {
                 "file": "@@//:MODULE.bazel",
-                "line": 138,
+                "line": 141,
                 "column": 13
               }
             }
@@ -119,7 +120,7 @@
           "usingModule": "<root>",
           "location": {
             "file": "@@//:MODULE.bazel",
-            "line": 107,
+            "line": 110,
             "column": 35
           },
           "imports": {
@@ -136,7 +137,7 @@
           "usingModule": "<root>",
           "location": {
             "file": "@@//:MODULE.bazel",
-            "line": 153,
+            "line": 156,
             "column": 29
           },
           "imports": {
@@ -153,7 +154,7 @@
           "usingModule": "<root>",
           "location": {
             "file": "@@//:MODULE.bazel",
-            "line": 165,
+            "line": 168,
             "column": 23
           },
           "imports": {
@@ -169,7 +170,7 @@
               "devDependency": false,
               "location": {
                 "file": "@@//:MODULE.bazel",
-                "line": 166,
+                "line": 169,
                 "column": 17
               }
             }
@@ -1621,7 +1622,8 @@
                 "-p1"
               ],
               "patches": [
-                "@@//bazel/bazel_clang_tidy:0001_Add_hdrs.patch"
+                "@@//bazel/bazel_clang_tidy:0001_Add_hdrs.patch",
+                "@@//bazel/bazel_clang_tidy:0002-Add-a-workaround-for-a-bug-in-clang-tidy-16-and-C-20.patch"
               ],
               "sha256": "89c198a9f544beac119bb41904d16d8870686ccb5fe946442c1576934c9e6869",
               "strip_prefix": "bazel_clang_tidy-d2aecc583d14c9554febeab185833c1e8cce5384",

+ 44 - 0
bazel/bazel_clang_tidy/0002-Add-a-workaround-for-a-bug-in-clang-tidy-16-and-C-20.patch

@@ -0,0 +1,44 @@
+From f383ddf21542762b883cfa1fb39599e094647fc5 Mon Sep 17 00:00:00 2001
+From: Chandler Carruth <chandlerc@gmail.com>
+Date: Sat, 27 Jan 2024 00:16:09 +0000
+Subject: [PATCH] Add a workaround for a bug in clang-tidy 16 and C++20.
+
+In that mode, the Bazel suppression of a Clang warning doesn't actually
+suppress the clang-tidy check. Neither does disabling the check in
+a `.clang-tidy` config file. Instead, disable it directly on the command
+line when running `clang-tidy`. The clang-tidy bug is:
+https://github.com/llvm/llvm-project/issues/61969
+
+This is harmless for other versions as this check is not useful with
+most Bazel configurations, and can be achieved with a compiler warning
+if truly needed.
+
+Fixes #29
+---
+ clang_tidy/run_clang_tidy.sh | 12 ++++++++++++
+ 1 file changed, 12 insertions(+)
+
+diff --git a/clang_tidy/run_clang_tidy.sh b/clang_tidy/run_clang_tidy.sh
+index aa6598c..5cddb59 100755
+--- a/clang_tidy/run_clang_tidy.sh
++++ b/clang_tidy/run_clang_tidy.sh
+@@ -25,4 +25,16 @@ test -e .clang-tidy || ln -s -f $CONFIG .clang-tidy
+ logfile="$(mktemp)"
+ trap 'if (($?)); then cat "$logfile" 1>&2; fi; rm "$logfile"' EXIT
+ 
++# Prepend a flag-based disabling of a check that has a serious bug in
++# clang-tidy 16 when used with C++20. Bazel always violates this check and the
++# warning is typically disabled, but that warning disablement doesn't work
++# correctly in this circumstance and so we need to disable it at the clang-tidy
++# level both as a check and from `warnings-as-errors` to avoid it getting
++# re-promoted to an error. See the clang-tidy bug here for details:
++# https://github.com/llvm/llvm-project/issues/61969
++set -- \
++  --checks=-clang-diagnostic-builtin-macro-redefined \
++  --warnings-as-errors=-clang-diagnostic-builtin-macro-redefined \
++   "$@"
++
+ "${CLANG_TIDY_BIN}" "$@" >"$logfile" 2>&1
+-- 
+2.43.0
+

+ 1 - 1
bazel/cc_toolchains/clang_cc_toolchain_config.bzl

@@ -100,7 +100,7 @@ def _impl(ctx):
         for name in [ACTION_NAMES.strip]
     ]
 
-    std_compile_flags = ["-std=c++17"]
+    std_compile_flags = ["-std=c++20"]
 
     # libc++ is only used on non-Windows platforms.
     if ctx.attr.target_os != "windows":