llvm-project/llvm/lib/Support/DynamicAPInt.cpp
Andrew Rogers 0563186a76
[llvm] properly guard dump methods in Support lib classes (#139938)
## Purpose
Add proper preprocessor guards for all `dump()` methods in the LLVM
support library. This change ensures these methods are not part of the
public ABI for release builds.

## Overview
* Annotates all `dump` methods in Support and ADT source with the
`LLVM_DUMP_METHOD` macro.
* Conditionally includes all `dump` method definitions in Support and
ADT source so they are only present on debug/assert builds and when
`LLVM_ENABLE_DUMP` is explicitly defined.

NOTE: For many of these `dump` methods, the implementation was already
properly guarded but the declaration in the header file was not.

## Background
This PR is a redo of #139804 with some changes to fix clang and unit
test build breaks.

This issue was raised in comments on #136629. I am addressing it as a
separate change since it is independent from the changes being made in
that PR.

According to [this
documentation](https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Support/Compiler.h#L637),
`dump` methods should be annotated with `LLVM_DUMP_METHOD` and
conditionally included as follows:
```
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  LLVM_DUMP_METHOD void dump() const;
#endif
```

## Validation
* Local release build succeeds.
* CI
2025-05-14 13:48:45 -07:00

38 lines
1.2 KiB
C++

//===- DynamicAPInt.cpp - DynamicAPInt Implementation -----------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/DynamicAPInt.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
hash_code llvm::hash_value(const DynamicAPInt &X) {
if (X.isSmall())
return llvm::hash_value(X.getSmall());
return detail::hash_value(X.getLarge());
}
void DynamicAPInt::static_assert_layout() {
constexpr size_t ValLargeOffset =
offsetof(DynamicAPInt, ValLarge.Val.BitWidth);
constexpr size_t ValSmallOffset = offsetof(DynamicAPInt, ValSmall);
constexpr size_t ValSmallSize = sizeof(ValSmall);
static_assert(ValLargeOffset >= ValSmallOffset + ValSmallSize);
}
raw_ostream &DynamicAPInt::print(raw_ostream &OS) const {
if (isSmall())
return OS << ValSmall;
return OS << ValLarge;
}
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD void DynamicAPInt::dump() const { print(dbgs()); }
#endif