Skip to content

Commit 343643b

Browse files
committed
clang-format: Understand #defines defining system includes.
Before: #define MY_IMPORT < a / b > After: #define MY_IMPORT <a/b> llvm-svn: 215527
1 parent 51bbd01 commit 343643b

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,6 @@ class AnnotatingParser {
497497
}
498498

499499
void parseIncludeDirective() {
500-
next();
501500
if (CurrentToken && CurrentToken->is(tok::less)) {
502501
next();
503502
while (CurrentToken) {
@@ -554,6 +553,7 @@ class AnnotatingParser {
554553
switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
555554
case tok::pp_include:
556555
case tok::pp_import:
556+
next();
557557
parseIncludeDirective();
558558
break;
559559
case tok::pp_error:
@@ -587,8 +587,18 @@ class AnnotatingParser {
587587
// should not break the line).
588588
IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
589589
if (Info && Info->getPPKeywordID() == tok::pp_import &&
590-
CurrentToken->Next && CurrentToken->Next->is(tok::string_literal))
590+
CurrentToken->Next && CurrentToken->Next->is(tok::string_literal)) {
591+
next();
591592
parseIncludeDirective();
593+
return LT_Other;
594+
}
595+
596+
// If this line starts and ends in '<' and '>', respectively, it is likely
597+
// part of "#define <a/b.h>".
598+
if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
599+
parseIncludeDirective();
600+
return LT_Other;
601+
}
592602

593603
while (CurrentToken) {
594604
if (CurrentToken->is(tok::kw_virtual))

clang/unittests/Format/FormatTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5234,6 +5234,8 @@ TEST_F(FormatTest, HandlesIncludeDirectives) {
52345234
"#include <strstream>\n"
52355235
"#endif");
52365236

5237+
verifyFormat("#define MY_IMPORT <a/b>");
5238+
52375239
// Protocol buffer definition or missing "#".
52385240
verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
52395241
getLLVMStyleWithColumns(30));

0 commit comments

Comments
 (0)