r193683 - clang-format: Fix whitespaces in include directives.

Daniel Jasper djasper at google.com
Wed Oct 30 06:54:53 PDT 2013


Author: djasper
Date: Wed Oct 30 08:54:53 2013
New Revision: 193683

URL: http://llvm.org/viewvc/llvm-project?rev=193683&view=rev
Log:
clang-format: Fix whitespaces in include directives.

Before (clang-format wouldn't change):
  #include  "a.h"
  #include<a>

After:
  #include "a.h"
  #include <a>

This fixes llvm.org/PR16151.

Modified:
    cfe/trunk/lib/Format/ContinuationIndenter.cpp
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=193683&r1=193682&r2=193683&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Wed Oct 30 08:54:53 2013
@@ -202,7 +202,11 @@ unsigned ContinuationIndenter::addTokenT
                                                unsigned ExtraSpaces) {
   const FormatToken &Current = *State.NextToken;
 
-  if (State.Stack.size() == 0 || Current.Type == TT_ImplicitStringLiteral) {
+  if (State.Stack.size() == 0 ||
+      (Current.Type == TT_ImplicitStringLiteral &&
+       (Current.Previous->Tok.getIdentifierInfo() == NULL ||
+        Current.Previous->Tok.getIdentifierInfo()->getPPKeywordID() ==
+            tok::pp_not_keyword))) {
     // FIXME: Is this correct?
     int WhitespaceLength = SourceMgr.getSpellingColumnNumber(
                                State.NextToken->WhitespaceRange.getEnd()) -
@@ -700,6 +704,10 @@ unsigned ContinuationIndenter::breakProt
   if (Current.Type != TT_BlockComment && Current.IsMultiline)
     return addMultilineToken(Current, State);
 
+  // Don't break implicit string literals.
+  if (Current.Type == TT_ImplicitStringLiteral)
+    return 0;
+
   if (!Current.isOneOf(tok::string_literal, tok::wide_string_literal,
                        tok::utf8_string_literal, tok::utf16_string_literal,
                        tok::utf32_string_literal, tok::comment))

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=193683&r1=193682&r2=193683&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Wed Oct 30 08:54:53 2013
@@ -1450,7 +1450,8 @@ bool TokenAnnotator::canBreakBefore(cons
                                   Left.Previous->Type == TT_CastRParen))
       return false;
   }
-
+  if (Right.Type == TT_ImplicitStringLiteral)
+    return false;
   if (Right.isTrailingComment())
     // We rely on MustBreakBefore being set correctly here as we should not
     // change the "binding" behavior of a comment.

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=193683&r1=193682&r2=193683&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Oct 30 08:54:53 2013
@@ -668,8 +668,7 @@ TEST_F(FormatTest, UnderstandsSingleLine
                "#include \"a/b/c\" // comment");
   verifyFormat("#include <a>     // comment\n"
                "#include <a/b/c> // comment");
-  EXPECT_EQ("#include \\\n"
-            "  \"a\"            // comment\n"
+  EXPECT_EQ("#include \"a\"     // comment\n"
             "#include \"a/b/c\" // comment",
             format("#include \\\n"
                    "  \"a\" // comment\n"
@@ -4295,6 +4294,8 @@ TEST_F(FormatTest, HandlesIncludeDirecti
                "#include \"some long include\" // with a comment\n"
                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
                getLLVMStyleWithColumns(35));
+  EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
+  EXPECT_EQ("#include <a>", format("#include<a>"));
 
   verifyFormat("#import <string>");
   verifyFormat("#import <a/b/c.h>");





More information about the cfe-commits mailing list