[cfe-commits] r170910 - in /cfe/trunk: lib/Format/Format.cpp unittests/Format/FormatTest.cpp

Daniel Jasper djasper at google.com
Fri Dec 21 09:58:39 PST 2012


Author: djasper
Date: Fri Dec 21 11:58:39 2012
New Revision: 170910

URL: http://llvm.org/viewvc/llvm-project?rev=170910&view=rev
Log:
clang-format: No spaces around directory specifiers

This fixes PR14683. We used to format like this:
  #include <a / b>

And this patch changes this to:
  #include <a/b>

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

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=170910&r1=170909&r2=170910&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Fri Dec 21 11:58:39 2012
@@ -42,6 +42,7 @@
     TT_CtorInitializerColon,
     TT_LineComment,
     TT_BlockComment,
+    TT_DirectorySeparator,
     TT_ObjCMethodSpecifier
   };
 
@@ -560,7 +561,36 @@
       }
     }
 
+    void parseIncludeDirective() {
+      while (Index < Tokens.size()) {
+        if (Tokens[Index].Tok.is(tok::slash))
+          Annotations[Index].Type = TokenAnnotation::TT_DirectorySeparator;
+        else if (Tokens[Index].Tok.is(tok::less))
+          Annotations[Index].Type = TokenAnnotation::TT_TemplateOpener;
+        else if (Tokens[Index].Tok.is(tok::greater))
+          Annotations[Index].Type = TokenAnnotation::TT_TemplateCloser;
+        next();
+      }
+    }
+
+    void parsePreprocessorDirective() {
+      next();
+      if (Index >= Tokens.size())
+        return;
+      switch (Tokens[Index].Tok.getIdentifierInfo()->getPPKeywordID()) {
+      case tok::pp_include:
+        parseIncludeDirective();
+        break;
+      default:
+        break;
+      }
+    }
+
     void parseLine() {
+      if (Tokens[Index].Tok.is(tok::hash)) {
+        parsePreprocessorDirective();
+        return;
+      }
       while (Index < Tokens.size()) {
         consumeToken();
       }
@@ -639,6 +669,10 @@
         else
           Annotation.SpaceRequiredBefore = false;
       } else if (
+          Annotation.Type == TokenAnnotation::TT_DirectorySeparator ||
+          Annotations[i - 1].Type == TokenAnnotation::TT_DirectorySeparator) {
+        Annotation.SpaceRequiredBefore = false;
+      } else if (
           Annotation.Type == TokenAnnotation::TT_BinaryOperator ||
           Annotations[i - 1].Type == TokenAnnotation::TT_BinaryOperator) {
         Annotation.SpaceRequiredBefore = true;
@@ -714,7 +748,7 @@
   }
 
   bool isBinaryOperator(const FormatToken &Tok) {
-    // Comma is a binary operator, but does not behave a such wrt. formatting.
+    // Comma is a binary operator, but does not behave as such wrt. formatting.
     return getBinOpPrecedence(Tok.Tok.getKind(), true, true) > prec::Comma;
   }
 
@@ -865,7 +899,8 @@
     }
 
     if (FormatTok.Tok.is(tok::raw_identifier)) {
-      const IdentifierInfo &Info = IdentTable.get(tokenText(FormatTok.Tok));
+      IdentifierInfo &Info = IdentTable.get(tokenText(FormatTok.Tok));
+      FormatTok.Tok.setIdentifierInfo(&Info);
       FormatTok.Tok.setKind(Info.getTokenID());
     }
 

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=170910&r1=170909&r2=170910&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Dec 21 11:58:39 2012
@@ -613,6 +613,7 @@
 
 TEST_F(FormatTest, HandlesIncludeDirectives) {
   EXPECT_EQ("#include <string>\n", format("#include <string>\n"));
+  EXPECT_EQ("#include <a/b/c.h>\n", format("#include <a/b/c.h>\n"));
   EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n"));
   EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));
   EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));





More information about the cfe-commits mailing list