[llvm] 11ce3d9 - [llvm] Add LLVM_CTOR_NODISCARD (#65418)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 5 16:47:08 PDT 2023


Author: Jonas Devlieghere
Date: 2023-09-05T16:47:04-07:00
New Revision: 11ce3d99b7654a2211ce6ab56eb9d0667efe0939

URL: https://github.com/llvm/llvm-project/commit/11ce3d99b7654a2211ce6ab56eb9d0667efe0939
DIFF: https://github.com/llvm/llvm-project/commit/11ce3d99b7654a2211ce6ab56eb9d0667efe0939.diff

LOG: [llvm] Add LLVM_CTOR_NODISCARD (#65418)

[[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.

Added: 
    

Modified: 
    llvm/include/llvm/MC/MCInstPrinter.h
    llvm/include/llvm/Support/Compiler.h

Removed: 
    


################################################################################
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