Skip to content

Commit 94f15ba

Browse files
nhukcvsapsai
authored andcommitted
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 (cherry picked from commit 5808077)
1 parent fe26c5c commit 94f15ba

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.
@@ -120,13 +129,9 @@
120129
#endif
121130

122131
/// LLVM_NODISCARD - Warn if a type or return value is discarded.
123-
#if __cplusplus > 201402L && __has_cpp_attribute(nodiscard)
132+
#if __cplusplus > 201402L && LLVM_HAS_CPP_ATTRIBUTE(nodiscard)
124133
#define LLVM_NODISCARD [[nodiscard]]
125-
#elif !__cplusplus
126-
// Workaround for llvm.org/PR23435, since clang 3.6 and below emit a spurious
127-
// error when __has_cpp_attribute is given a scoped attribute in C mode.
128-
#define LLVM_NODISCARD
129-
#elif __has_cpp_attribute(clang::warn_unused_result)
134+
#elif LLVM_HAS_CPP_ATTRIBUTE(clang::warn_unused_result)
130135
#define LLVM_NODISCARD [[clang::warn_unused_result]]
131136
#else
132137
#define LLVM_NODISCARD
@@ -139,7 +144,7 @@
139144
// The clang-tidy check bugprone-use-after-move recognizes this attribute as a
140145
// marker that a moved-from object has left the indeterminate state and can be
141146
// reused.
142-
#if __has_cpp_attribute(clang::reinitializes)
147+
#if LLVM_HAS_CPP_ATTRIBUTE(clang::reinitializes)
143148
#define LLVM_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
144149
#else
145150
#define LLVM_ATTRIBUTE_REINITIALIZES
@@ -240,23 +245,21 @@
240245
#endif
241246

242247
/// LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
243-
#if __cplusplus > 201402L && __has_cpp_attribute(fallthrough)
248+
#if __cplusplus > 201402L && LLVM_HAS_CPP_ATTRIBUTE(fallthrough)
244249
#define LLVM_FALLTHROUGH [[fallthrough]]
245-
#elif __has_cpp_attribute(gnu::fallthrough)
250+
#elif LLVM_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
246251
#define LLVM_FALLTHROUGH [[gnu::fallthrough]]
247-
#elif !__cplusplus
248-
// Workaround for llvm.org/PR23435, since clang 3.6 and below emit a spurious
249-
// error when __has_cpp_attribute is given a scoped attribute in C mode.
250-
#define LLVM_FALLTHROUGH
251-
#elif __has_cpp_attribute(clang::fallthrough)
252+
#elif __has_attribute(fallthrough)
253+
#define LLVM_FALLTHROUGH __attribute__((fallthrough))
254+
#elif LLVM_HAS_CPP_ATTRIBUTE(clang::fallthrough)
252255
#define LLVM_FALLTHROUGH [[clang::fallthrough]]
253256
#else
254257
#define LLVM_FALLTHROUGH
255258
#endif
256259

257260
/// LLVM_REQUIRE_CONSTANT_INITIALIZATION - Apply this to globals to ensure that
258261
/// they are constant initialized.
259-
#if __has_cpp_attribute(clang::require_constant_initialization)
262+
#if LLVM_HAS_CPP_ATTRIBUTE(clang::require_constant_initialization)
260263
#define LLVM_REQUIRE_CONSTANT_INITIALIZATION \
261264
[[clang::require_constant_initialization]]
262265
#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-
/* FALLTHROUGH */
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-
/* FALLTHROUGH */
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-
/* fallthrough */
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)