[cfe-dev] Confusion about: ASTContext::toCharUnitsFromBits usage

Jeroen Dobbelaere via cfe-dev cfe-dev at lists.llvm.org
Thu Jun 20 07:13:44 PDT 2019


Hi all,

ASTContext::toCharUnitsFromBits is implemented as follows in clang/lib/AST/ASTContext.cpp [1]:

---
  /// toCharUnitsFromBits - Convert a size in bits to a size in characters.
  CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const {
     return CharUnits::fromQuantity(BitSize / getCharWidth());
  }
---
 
Based on the comment, I would assume that the goal is to convert the BitSize into a number of characters
that can contain the number of bits. 
The implementation though is rounding downwards, to the number of characters fitting in the BitSize.

On a number of places [2], the function is called with a guarantee that the BitSize is a multiple of the CharWidth.
On a recent change though [3], the patch depends on the downwards rounding behavior.

Is this downwards rounding behavior the expected behavior for this function ? 

Thanks,

Jeroen Dobbelaere
----
[1] https://clang.llvm.org/doxygen/ASTContext_8cpp_source.html#l02161
[2] https://clang.llvm.org/doxygen/RecordLayoutBuilder_8cpp_source.html#l00769
      https://clang.llvm.org/doxygen/RecordLayoutBuilder_8cpp_source.html#l00781
[3] https://reviews.llvm.org/D63371





More information about the cfe-dev mailing list