blob: d3dd046bf2cb4ddeb443eda465b86a6c496b711c [file] [log] [blame]
From 8deeaa1a3f36b8b4bfce73ba97be38b269c3342c Mon Sep 17 00:00:00 2001
From: Vegard Nossum <>
Date: Mon, 6 Apr 2020 20:09:37 -0700
Subject: [PATCH] compiler.h: fix error in BUILD_BUG_ON() reporting
commit af9c5d2e3b355854ff0e4acfbfbfadcd5198a349 upstream.
compiletime_assert() uses __LINE__ to create a unique function name. This
means that if you have more than one BUILD_BUG_ON() in the same source
line (which can happen if they appear e.g. in a macro), then the error
message from the compiler might output the wrong condition.
For this source file:
#include <linux/build_bug.h>
#define macro() \
void foo()
gcc would output:
./include/linux/compiler.h:350:38: error: call to `__compiletime_assert_9' declared with attribute error: BUILD_BUG_ON failed: 0
_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
However, it was not the BUILD_BUG_ON(0) that failed, so it should say 1
instead of 0. With this patch, we use __COUNTER__ instead of __LINE__, so
each BUILD_BUG_ON() gets a different function name and the correct
condition is printed:
./include/linux/compiler.h:350:38: error: call to `__compiletime_assert_0' declared with attribute error: BUILD_BUG_ON failed: 1
_compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
Signed-off-by: Vegard Nossum <>
Signed-off-by: Andrew Morton <>
Reviewed-by: Masahiro Yamada <>
Reviewed-by: Daniel Santos <>
Cc: Rasmus Villemoes <>
Cc: Ian Abbott <>
Cc: Joe Perches <>
Signed-off-by: Linus Torvalds <>
Signed-off-by: Paul Gortmaker <>
diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index 8aaf7cd026b0..18ac4fc767eb 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -342,7 +342,7 @@ static inline void *offset_to_ptr(const int *off)
* compiler has support to do so.
#define compiletime_assert(condition, msg) \
- _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
+ _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
#define compiletime_assert_atomic_type(t) \
compiletime_assert(__native_word(t), \