[PATCH] D78561: [NFC] Improve Debugging experience with SmallVector

Tyker via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 21 09:10:14 PDT 2020


Tyker updated this revision to Diff 259017.
Tyker edited the summary of this revision.

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

https://reviews.llvm.org/D78561

Files:
  llvm/include/llvm/ADT/SmallVector.h
  llvm/include/llvm/Support/Compiler.h


Index: llvm/include/llvm/Support/Compiler.h
===================================================================
--- llvm/include/llvm/Support/Compiler.h
+++ llvm/include/llvm/Support/Compiler.h
@@ -503,6 +503,20 @@
 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
 #endif
 
+/// This macro will mark the function as used if the host compiler supports it
+/// and the build has asserts enabled. Functions marked with this macro can be
+/// called from the debugger during debugging. This macro should be used on
+/// function that are useful for debugging and wouldn't be emitted unless they
+/// are used, usually getter-like member function of template classes. Example:
+/// SmallVectors's operator[] is usefull for debugging, doesn't change the state
+/// of the SmallVector and will not be generated it is used because it is
+/// templated.
+#if !defined(NDEBUG)
+#define LLVM_USED_FOR_DEBUG LLVM_ATTRIBUTE_USED
+#else
+#define LLVM_USED_FOR_DEBUG
+#endif
+
 /// \macro LLVM_PRETTY_FUNCTION
 /// Gets a user-friendly looking function signature for the current scope
 /// using the best available method on each platform.  The exact format of the
Index: llvm/include/llvm/ADT/SmallVector.h
===================================================================
--- llvm/include/llvm/ADT/SmallVector.h
+++ llvm/include/llvm/ADT/SmallVector.h
@@ -49,7 +49,7 @@
   void grow_pod(void *FirstEl, size_t MinCapacity, size_t TSize);
 
 public:
-  size_t size() const { return Size; }
+  LLVM_USED_FOR_DEBUG size_t size() const { return Size; }
   size_t capacity() const { return Capacity; }
 
   LLVM_NODISCARD bool empty() const { return !Size; }
@@ -145,11 +145,11 @@
   /// Return a pointer to the vector's buffer, even if empty().
   const_pointer data() const { return const_pointer(begin()); }
 
-  reference operator[](size_type idx) {
+  LLVM_USED_FOR_DEBUG reference operator[](size_type idx) {
     assert(idx < size());
     return begin()[idx];
   }
-  const_reference operator[](size_type idx) const {
+  LLVM_USED_FOR_DEBUG const_reference operator[](size_type idx) const {
     assert(idx < size());
     return begin()[idx];
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D78561.259017.patch
Type: text/x-patch
Size: 2141 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200421/b26b3b7d/attachment.bin>


More information about the llvm-commits mailing list