]> granicus.if.org Git - clang/commitdiff
Also align trailing line comments in include directives.
authorDaniel Jasper <djasper@google.com>
Fri, 18 Jan 2013 09:19:33 +0000 (09:19 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 18 Jan 2013 09:19:33 +0000 (09:19 +0000)
Before:
  #include <a> // for x
  #include <a/b/c> // for yz
After:
  #include <a>     // for x
  #include <a/b/c> // for yz

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172799 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/Format.cpp
unittests/Format/FormatTest.cpp

index 39d70d3e5a4efc135d2a456d7e5336c7ff129992..50df593d85f912ea93409680f662e59d69d18af6 100644 (file)
@@ -350,13 +350,15 @@ public:
 
     // Start iterating at 1 as we have correctly formatted of Token #0 above.
     while (State.NextToken != NULL) {
-      if (State.NextToken->Type == TT_ImplicitStringLiteral)
-        // We will not touch the rest of the white space in this
-        // \c UnwrappedLine. The returned value can also not matter, as we
-        // cannot continue an top-level implicit string literal on the next
-        // line.
-        return 0;
-      if (Line.Last->TotalLength <= getColumnLimit() - FirstIndent) {
+      if (State.NextToken->Type == TT_ImplicitStringLiteral) {
+        // Calculating the column is important for aligning trailing comments.
+        // FIXME: This does not seem to happen in conjunction with escaped
+        // newlines. If it does, fix!
+        State.Column += State.NextToken->FormatTok.WhiteSpaceLength +
+                        State.NextToken->FormatTok.TokenLength;
+        State.NextToken = State.NextToken->Children.empty() ? NULL :
+                          &State.NextToken->Children[0];
+      } else if (Line.Last->TotalLength <= getColumnLimit() - FirstIndent) {
         addTokenToState(false, false, State);
       } else {
         unsigned NoBreak = calcPenalty(State, false, UINT_MAX);
@@ -1096,7 +1098,9 @@ public:
       if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
         next();
         while (CurrentToken != NULL) {
-          CurrentToken->Type = TT_ImplicitStringLiteral;
+          if (CurrentToken->isNot(tok::comment) ||
+              !CurrentToken->Children.empty())
+            CurrentToken->Type = TT_ImplicitStringLiteral;
           next();
         }
       } else {
index 7013eb14172ad593c76ec99b6bb634e158c1cf24..51904d7d3fada796dcf159eaaa699f7368d0d5d0 100644 (file)
@@ -386,7 +386,10 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
       "int bbbbbbbbbbbbbbbbbbbbb;   // comment\n"
       "int ccccccccccccccccccc;     // comment");
 
-
+  verifyFormat("#include \"a\"     // comment\n"
+               "#include \"a/b/c\" // comment");
+  verifyFormat("#include <a>     // comment\n"
+               "#include <a/b/c> // comment");
 
   verifyFormat("enum E {\n"
                "  // comment\n"