From 346587d1455ea272c6f26576c84af2f8327dadda Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Fri, 10 Feb 2017 19:36:52 +0000 Subject: [PATCH] clang-format: don't break code using __has_include, PR31908 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@294772 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/FormatToken.h | 4 ++++ lib/Format/TokenAnnotator.cpp | 22 +++++++++++++++++++++- unittests/Format/FormatTest.cpp | 5 +++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h index d6a2ff286a..910ddca998 100644 --- a/lib/Format/FormatToken.h +++ b/lib/Format/FormatToken.h @@ -630,6 +630,8 @@ struct AdditionalKeywords { kw_synchronized = &IdentTable.get("synchronized"); kw_throws = &IdentTable.get("throws"); kw___except = &IdentTable.get("__except"); + kw___has_include = &IdentTable.get("__has_include"); + kw___has_include_next = &IdentTable.get("__has_include_next"); kw_mark = &IdentTable.get("mark"); @@ -656,6 +658,8 @@ struct AdditionalKeywords { IdentifierInfo *kw_NS_ENUM; IdentifierInfo *kw_NS_OPTIONS; IdentifierInfo *kw___except; + IdentifierInfo *kw___has_include; + IdentifierInfo *kw___has_include_next; // JavaScript keywords. IdentifierInfo *kw_as; diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 726383eda9..ac3c9df5fa 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -684,6 +684,12 @@ private: if (Contexts.back().IsForEachMacro) Contexts.back().IsExpression = true; break; + case tok::identifier: + if (Tok->isOneOf(Keywords.kw___has_include, + Keywords.kw___has_include_next)) { + parseHasInclude(); + } + break; default: break; } @@ -727,6 +733,14 @@ private: } } + void parseHasInclude() { + if (!CurrentToken || !CurrentToken->is(tok::l_paren)) + return; + next(); // '(' + parseIncludeDirective(); + next(); // ')' + } + LineType parsePreprocessorDirective() { bool IsFirstToken = CurrentToken->IsFirst; LineType Type = LT_PreprocessorDirective; @@ -777,8 +791,14 @@ private: default: break; } - while (CurrentToken) + while (CurrentToken) { + FormatToken *Tok = CurrentToken; next(); + if (Tok->isOneOf(Keywords.kw___has_include, + Keywords.kw___has_include_next)) { + parseHasInclude(); + } + } return Type; } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index e294af5ea3..72fd0df895 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -5364,6 +5364,11 @@ TEST_F(FormatTest, HandlesIncludeDirectives) { verifyFormat("#define MY_IMPORT "); + verifyFormat("#if __has_include()"); + verifyFormat("#if __has_include_next()"); + verifyFormat("#define F __has_include()"); + verifyFormat("#define F __has_include_next()"); + // Protocol buffer definition or missing "#". verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", getLLVMStyleWithColumns(30)); -- 2.40.0