aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Frontend/InitPreprocessor.cpp
diff options
context:
space:
mode:
authorAaron Ballman <aaron@aaronballman.com>2024-11-25 13:01:25 -0500
committerGitHub <noreply@github.com>2024-11-25 13:01:25 -0500
commit00770489e4299fe6ab99b1772127d84dfe222ffc (patch)
tree593c957364bcb04b8622ea57c9d7223fa46a8a6b /clang/lib/Frontend/InitPreprocessor.cpp
parent362d8fb2416ca3393b960eb158301d6f06dc5324 (diff)
downloadllvm-00770489e4299fe6ab99b1772127d84dfe222ffc.zip
llvm-00770489e4299fe6ab99b1772127d84dfe222ffc.tar.gz
llvm-00770489e4299fe6ab99b1772127d84dfe222ffc.tar.bz2
[C23] Fixed the value of BOOL_WIDTH (#117364)
The standard mandates that this returns the width of the type, which is the number of bits in the value. For bool, that's required to be `1` explicitly. Fixes #117348
Diffstat (limited to 'clang/lib/Frontend/InitPreprocessor.cpp')
-rw-r--r--clang/lib/Frontend/InitPreprocessor.cpp10
1 files changed, 9 insertions, 1 deletions
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp
index 9a0fdb1..9b611bf 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -1103,7 +1103,15 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
Builder.defineMacro("__CHAR_BIT__", Twine(TI.getCharWidth()));
- Builder.defineMacro("__BOOL_WIDTH__", Twine(TI.getBoolWidth()));
+ // The macro is specifying the number of bits in the width, not the number of
+ // bits the object requires for its in-memory representation, which is what
+ // getBoolWidth() will return. The bool/_Bool data type is only ever one bit
+ // wide. See C23 6.2.6.2p2 for the rules in C. Note that
+ // C++23 [basic.fundamental]p10 allows an implementation-defined value
+ // representation for bool; when lowering to LLVM, Clang represents bool as an
+ // i8 in memory but as an i1 when the value is needed, so '1' is also correct
+ // for C++.
+ Builder.defineMacro("__BOOL_WIDTH__", "1");
Builder.defineMacro("__SHRT_WIDTH__", Twine(TI.getShortWidth()));
Builder.defineMacro("__INT_WIDTH__", Twine(TI.getIntWidth()));
Builder.defineMacro("__LONG_WIDTH__", Twine(TI.getLongWidth()));