Skip to content

Commit 9daceef

Browse files
committed
[clang-format] Add TT_AfterPPDirective for better annotation
For now, we only need to annotate the token after #error or #warning. Fixes #117706.
1 parent 34f0611 commit 9daceef

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace clang {
2525
namespace format {
2626

2727
#define LIST_TOKEN_TYPES \
28+
TYPE(AfterPPDirective) \
2829
TYPE(ArrayInitializerLSquare) \
2930
TYPE(ArraySubscriptLSquare) \
3031
TYPE(AttributeColon) \

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4941,6 +4941,10 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
49414941
Right.is(TT_ModulePartitionColon)) {
49424942
return true;
49434943
}
4944+
4945+
if (Right.is(TT_AfterPPDirective))
4946+
return true;
4947+
49444948
// No space between import foo:bar but keep a space between import :bar;
49454949
if (Left.is(tok::identifier) && Right.is(TT_ModulePartitionColon))
49464950
return false;

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,12 @@ void UnwrappedLineParser::parsePPDirective() {
10321032
case tok::pp_pragma:
10331033
parsePPPragma();
10341034
break;
1035+
case tok::pp_error:
1036+
case tok::pp_warning:
1037+
nextToken();
1038+
if (!eof() && Style.isCpp())
1039+
FormatTok->setFinalizedType(TT_AfterPPDirective);
1040+
[[fallthrough]];
10351041
default:
10361042
parsePPUnknown();
10371043
break;
@@ -1211,9 +1217,8 @@ void UnwrappedLineParser::parsePPPragma() {
12111217
}
12121218

12131219
void UnwrappedLineParser::parsePPUnknown() {
1214-
do {
1220+
while (!eof())
12151221
nextToken();
1216-
} while (!eof());
12171222
if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
12181223
Line->Level += PPBranchLevel + 1;
12191224
addUnwrappedLine();

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3635,6 +3635,13 @@ TEST_F(TokenAnnotatorTest, SwitchInMacroArgument) {
36353635
EXPECT_TOKEN(Tokens[9], tok::l_brace, TT_FunctionLBrace);
36363636
}
36373637

3638+
TEST_F(TokenAnnotatorTest, AfterPPDirective) {
3639+
auto Tokens = annotate("#error -- My error message");
3640+
3641+
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3642+
EXPECT_TOKEN(Tokens[2], tok::minusminus, TT_AfterPPDirective);
3643+
}
3644+
36383645
} // namespace
36393646
} // namespace format
36403647
} // namespace clang

0 commit comments

Comments
 (0)