[llvm] [llvm] Add LLVM_CTOR_NODISCARD (PR #65418)

Jonas Devlieghere via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 5 15:38:25 PDT 2023


https://github.com/JDevlieghere updated https://github.com/llvm/llvm-project/pull/65418:

>From 9d3de78e484590a5fefa0cbd5dfb6fbbda7671aa Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Tue, 5 Sep 2023 15:28:07 -0700
Subject: [PATCH] [llvm] Add LLVM_CTOR_NODISCARD

[[nodiscard]] on constructors is a defect report against C++17. That
means that it should be applied retroactively, though older compilers
might not know about it and emit warnings. This adds a
back-compatibility macro.
---
 llvm/include/llvm/MC/MCInstPrinter.h | 5 +++--
 llvm/include/llvm/Support/Compiler.h | 6 ++++++
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/MC/MCInstPrinter.h b/llvm/include/llvm/MC/MCInstPrinter.h
index 8c628256eecf404..d51379c9305cc51 100644
--- a/llvm/include/llvm/MC/MCInstPrinter.h
+++ b/llvm/include/llvm/MC/MCInstPrinter.h
@@ -9,6 +9,7 @@
 #ifndef LLVM_MC_MCINSTPRINTER_H
 #define LLVM_MC_MCINSTPRINTER_H
 
+#include "llvm/Support/Compiler.h"
 #include "llvm/Support/Format.h"
 #include <cstdint>
 
@@ -97,8 +98,8 @@ class MCInstPrinter {
 
   class WithMarkup {
   public:
-    WithMarkup(raw_ostream &OS, Markup M, bool EnableMarkup,
-                             bool EnableColor);
+    LLVM_CTOR_NODISCARD WithMarkup(raw_ostream &OS, Markup M, bool EnableMarkup,
+                                   bool EnableColor);
     ~WithMarkup();
 
     template <typename T> WithMarkup &operator<<(T &O) {
diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h
index 10d5cec231a523c..12afe90f8facd47 100644
--- a/llvm/include/llvm/Support/Compiler.h
+++ b/llvm/include/llvm/Support/Compiler.h
@@ -307,6 +307,12 @@
 #define LLVM_GSL_POINTER
 #endif
 
+#if LLVM_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L
+#define LLVM_CTOR_NODISCARD [[nodiscard]]
+#else
+#define LLVM_CTOR_NODISCARD
+#endif
+
 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
 /// pedantic diagnostics.
 #ifdef __GNUC__



More information about the llvm-commits mailing list