Skip to content

Commit 5808077

Browse files
committed
Allow Compiler.h to be included in C files and fix fallthrough warnings
Summary: Since clang does not support comment style fallthrough annotations these should be switched to macros defined in Compiler.h. This requires some fixing to Compiler.h. Original patch: https://reviews.llvm.org/D66487 Reviewers: nickdesaulniers, aaron.ballman, xbolva00, rsmith Reviewed By: nickdesaulniers, aaron.ballman, rsmith Subscribers: rsmith, sfertile, ormris, hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D66609 llvm-svn: 369782
1 parent 5dca5ef commit 5808077

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

llvm/include/llvm/Support/Compiler.h

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
//===----------------------------------------------------------------------===//
88
//
99
// This file defines several macros, based on the current compiler. This allows
10-
// use of compiler-specific features in a way that remains portable.
10+
// use of compiler-specific features in a way that remains portable. This header
11+
// can be included from either C or C++.
1112
//
1213
//===----------------------------------------------------------------------===//
1314

@@ -16,7 +17,9 @@
1617

1718
#include "llvm/Config/llvm-config.h"
1819

20+
#ifdef __cplusplus
1921
#include <new>
22+
#endif
2023
#include <stddef.h>
2124

2225
#if defined(_MSC_VER)
@@ -35,14 +38,20 @@
3538
# define __has_attribute(x) 0
3639
#endif
3740

38-
#ifndef __has_cpp_attribute
39-
# define __has_cpp_attribute(x) 0
40-
#endif
41-
4241
#ifndef __has_builtin
4342
# define __has_builtin(x) 0
4443
#endif
4544

45+
// Only use __has_cpp_attribute in C++ mode. GCC defines __has_cpp_attribute in
46+
// C mode, but the :: in __has_cpp_attribute(scoped::attribute) is invalid.
47+
#ifndef LLVM_HAS_CPP_ATTRIBUTE
48+
#if defined(__cplusplus) && defined(__has_cpp_attribute)
49+
# define LLVM_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
50+
#else
51+
# define LLVM_HAS_CPP_ATTRIBUTE(x) 0
52+
#endif
53+
#endif
54+
4655
/// \macro LLVM_GNUC_PREREQ
4756
/// Extend the default __GNUC_PREREQ even if glibc's features.h isn't
4857
/// available.
@@ -128,13 +137,9 @@
128137
#endif
129138

130139
/// LLVM_NODISCARD - Warn if a type or return value is discarded.
131-
#if __cplusplus > 201402L && __has_cpp_attribute(nodiscard)
140+
#if __cplusplus > 201402L && LLVM_HAS_CPP_ATTRIBUTE(nodiscard)
132141
#define LLVM_NODISCARD [[nodiscard]]
133-
#elif !__cplusplus
134-
// Workaround for llvm.org/PR23435, since clang 3.6 and below emit a spurious
135-
// error when __has_cpp_attribute is given a scoped attribute in C mode.
136-
#define LLVM_NODISCARD
137-
#elif __has_cpp_attribute(clang::warn_unused_result)
142+
#elif LLVM_HAS_CPP_ATTRIBUTE(clang::warn_unused_result)
138143
#define LLVM_NODISCARD [[clang::warn_unused_result]]
139144
#else
140145
#define LLVM_NODISCARD
@@ -147,7 +152,7 @@
147152
// The clang-tidy check bugprone-use-after-move recognizes this attribute as a
148153
// marker that a moved-from object has left the indeterminate state and can be
149154
// reused.
150-
#if __has_cpp_attribute(clang::reinitializes)
155+
#if LLVM_HAS_CPP_ATTRIBUTE(clang::reinitializes)
151156
#define LLVM_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
152157
#else
153158
#define LLVM_ATTRIBUTE_REINITIALIZES
@@ -248,23 +253,21 @@
248253
#endif
249254

250255
/// LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
251-
#if __cplusplus > 201402L && __has_cpp_attribute(fallthrough)
256+
#if __cplusplus > 201402L && LLVM_HAS_CPP_ATTRIBUTE(fallthrough)
252257
#define LLVM_FALLTHROUGH [[fallthrough]]
253-
#elif __has_cpp_attribute(gnu::fallthrough)
258+
#elif LLVM_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
254259
#define LLVM_FALLTHROUGH [[gnu::fallthrough]]
255-
#elif !__cplusplus
256-
// Workaround for llvm.org/PR23435, since clang 3.6 and below emit a spurious
257-
// error when __has_cpp_attribute is given a scoped attribute in C mode.
258-
#define LLVM_FALLTHROUGH
259-
#elif __has_cpp_attribute(clang::fallthrough)
260+
#elif __has_attribute(fallthrough)
261+
#define LLVM_FALLTHROUGH __attribute__((fallthrough))
262+
#elif LLVM_HAS_CPP_ATTRIBUTE(clang::fallthrough)
260263
#define LLVM_FALLTHROUGH [[clang::fallthrough]]
261264
#else
262265
#define LLVM_FALLTHROUGH
263266
#endif
264267

265268
/// LLVM_REQUIRE_CONSTANT_INITIALIZATION - Apply this to globals to ensure that
266269
/// they are constant initialized.
267-
#if __has_cpp_attribute(clang::require_constant_initialization)
270+
#if LLVM_HAS_CPP_ATTRIBUTE(clang::require_constant_initialization)
268271
#define LLVM_REQUIRE_CONSTANT_INITIALIZATION \
269272
[[clang::require_constant_initialization]]
270273
#else
@@ -527,6 +530,7 @@ void AnnotateIgnoreWritesEnd(const char *file, int line);
527530
#define LLVM_ENABLE_EXCEPTIONS 1
528531
#endif
529532

533+
#ifdef __cplusplus
530534
namespace llvm {
531535

532536
/// Allocate a buffer of memory with the given size and alignment.
@@ -569,4 +573,5 @@ inline void deallocate_buffer(void *Ptr, size_t Size, size_t Alignment) {
569573

570574
} // End namespace llvm
571575

576+
#endif // __cplusplus
572577
#endif

llvm/lib/Support/regcomp.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "regex2.h"
4949

5050
#include "llvm/Config/config.h"
51+
#include "llvm/Support/Compiler.h"
5152

5253
/* character-class table */
5354
static struct cclass {
@@ -537,7 +538,7 @@ p_ere_exp(struct parse *p)
537538
break;
538539
case '{': /* okay as ordinary except if digit follows */
539540
REQUIRE(!MORE() || !isdigit((uch)PEEK()), REG_BADRPT);
540-
/* fall through */
541+
LLVM_FALLTHROUGH;
541542
default:
542543
ordinary(p, c);
543544
break;
@@ -733,7 +734,7 @@ p_simp_re(struct parse *p,
733734
break;
734735
case '*':
735736
REQUIRE(starordinary, REG_BADRPT);
736-
/* fall through */
737+
LLVM_FALLTHROUGH;
737738
default:
738739
ordinary(p, (char)c);
739740
break;
@@ -1635,7 +1636,7 @@ findmust(struct parse *p, struct re_guts *g)
16351636
return;
16361637
}
16371638
} while (OP(s) != O_QUEST && OP(s) != O_CH);
1638-
/* fall through */
1639+
LLVM_FALLTHROUGH;
16391640
default: /* things that break a sequence */
16401641
if (newlen > g->mlen) { /* ends one */
16411642
start = newstart;

0 commit comments

Comments
 (0)