From 3ed303aa18ac57a8ba38e7bd50a8cd3e48fecbc1 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 13 Aug 2014 08:29:18 +0000 Subject: [PATCH] clang-format: Understand #defines defining system includes. Before: #define MY_IMPORT < a / b > After: #define MY_IMPORT git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@215527 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/TokenAnnotator.cpp | 14 ++++++++++++-- unittests/Format/FormatTest.cpp | 2 ++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index b24573fffc..a366234873 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -497,7 +497,6 @@ private: } void parseIncludeDirective() { - next(); if (CurrentToken && CurrentToken->is(tok::less)) { next(); while (CurrentToken) { @@ -554,6 +553,7 @@ private: switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) { case tok::pp_include: case tok::pp_import: + next(); parseIncludeDirective(); break; case tok::pp_error: @@ -587,8 +587,18 @@ public: // should not break the line). IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo(); if (Info && Info->getPPKeywordID() == tok::pp_import && - CurrentToken->Next && CurrentToken->Next->is(tok::string_literal)) + CurrentToken->Next && CurrentToken->Next->is(tok::string_literal)) { + next(); parseIncludeDirective(); + return LT_Other; + } + + // If this line starts and ends in '<' and '>', respectively, it is likely + // part of "#define ". + if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) { + parseIncludeDirective(); + return LT_Other; + } while (CurrentToken) { if (CurrentToken->is(tok::kw_virtual)) diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index f36eede8fe..1bd52aa9b5 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -5234,6 +5234,8 @@ TEST_F(FormatTest, HandlesIncludeDirectives) { "#include \n" "#endif"); + verifyFormat("#define MY_IMPORT "); + // Protocol buffer definition or missing "#". verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", getLLVMStyleWithColumns(30)); -- 2.50.1