aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Lex/PPMacroExpansion.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'clang/lib/Lex/PPMacroExpansion.cpp')
-rw-r--r--clang/lib/Lex/PPMacroExpansion.cpp14
1 files changed, 13 insertions, 1 deletions
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp
index dd80ae5..5efa4b5 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -1735,7 +1735,19 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
Diag(getLastFPEvalPragmaLocation(), diag::note_pragma_entered_here);
}
} else if (II == Ident__COUNTER__) {
- // __COUNTER__ expands to a simple numeric value.
+ Diag(Tok.getLocation(),
+ getLangOpts().C2y ? diag::warn_counter : diag::ext_counter);
+ // __COUNTER__ expands to a simple numeric value that must be less than
+ // 2147483647.
+ constexpr uint32_t MaxPosValue = std::numeric_limits<int32_t>::max();
+ if (CounterValue > MaxPosValue) {
+ Diag(Tok.getLocation(), diag::err_counter_overflow);
+ // Retain the maximal value so we don't issue conversion-related
+ // diagnostics by overflowing into a long long. While this does produce
+ // a duplicate value, there's no way to ignore this error so there's no
+ // translation anyway.
+ CounterValue = MaxPosValue;
+ }
OS << CounterValue++;
Tok.setKind(tok::numeric_constant);
} else if (II == Ident__has_feature) {