jansvoboda11 created this revision.
jansvoboda11 added reviewers: Bigcheese, dexonsmith, arphaman.
jansvoboda11 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This patch adds a configuration point to the source minimizer to pad the output 
to the input size with whitespace. This is useful in Dxxxxxx.

Depends on D104459 <https://reviews.llvm.org/D104459>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D104462

Files:
  clang/include/clang/Lex/DependencyDirectivesSourceMinimizer.h
  clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp
  clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp

Index: clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp
===================================================================
--- clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp
+++ clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp
@@ -14,7 +14,15 @@
 using namespace clang;
 using namespace clang::minimize_source_to_dependency_directives;
 
-namespace clang {
+namespace {
+
+bool minimizeSourceToDependencyDirectives(
+    StringRef Input, SmallVectorImpl<char> &Out,
+    SmallVectorImpl<minimize_source_to_dependency_directives::Token> &Tokens) {
+  return minimizeSourceToDependencyDirectives(Input, Out, Tokens, nullptr,
+                                              SourceLocation(),
+                                              /*PadOutputToInputSize=*/true);
+}
 
 bool minimizeSourceToDependencyDirectives(StringRef Input,
                                           SmallVectorImpl<char> &Out) {
@@ -22,22 +30,22 @@
   return minimizeSourceToDependencyDirectives(Input, Out, Tokens);
 }
 
-} // end namespace clang
-
-namespace {
+StringRef rtrim(SmallVectorImpl<char> &Vec) {
+  return StringRef(Vec.data()).rtrim();
+}
 
 TEST(MinimizeSourceToDependencyDirectivesTest, Empty) {
   SmallVector<char, 128> Out;
   SmallVector<Token, 4> Tokens;
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("", Out, Tokens));
-  EXPECT_TRUE(Out.empty());
+  EXPECT_EQ("", rtrim(Out));
   ASSERT_EQ(1u, Tokens.size());
   ASSERT_EQ(pp_eof, Tokens.back().K);
 
   ASSERT_FALSE(
       minimizeSourceToDependencyDirectives("abc def\nxyz", Out, Tokens));
-  EXPECT_TRUE(Out.empty());
+  EXPECT_EQ("", rtrim(Out));
   ASSERT_EQ(1u, Tokens.size());
   ASSERT_EQ(pp_eof, Tokens.back().K);
 }
@@ -94,7 +102,7 @@
 
   ASSERT_FALSE(
       minimizeSourceToDependencyDirectives("#define MACRO", Out, Tokens));
-  EXPECT_STREQ("#define MACRO", Out.data());
+  EXPECT_EQ("#define MACRO", rtrim(Out));
   ASSERT_EQ(2u, Tokens.size());
   ASSERT_EQ(pp_define, Tokens.front().K);
 }
@@ -104,56 +112,56 @@
 
   ASSERT_FALSE(
       minimizeSourceToDependencyDirectives("#define MACRO\n\n\n", Out));
-  EXPECT_STREQ("#define MACRO\n", Out.data());
+  EXPECT_EQ("#define MACRO", rtrim(Out));
 
   ASSERT_FALSE(
       minimizeSourceToDependencyDirectives("#define MACRO \n\n\n", Out));
-  EXPECT_STREQ("#define MACRO\n", Out.data());
+  EXPECT_EQ("#define MACRO", rtrim(Out));
 
   ASSERT_FALSE(
       minimizeSourceToDependencyDirectives("#define MACRO a \n\n\n", Out));
-  EXPECT_STREQ("#define MACRO a\n", Out.data());
+  EXPECT_EQ("#define MACRO a", rtrim(Out));
 
   ASSERT_FALSE(
       minimizeSourceToDependencyDirectives("#define   MACRO\n\n\n", Out));
-  EXPECT_STREQ("#define MACRO\n", Out.data());
+  EXPECT_EQ("#define MACRO", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, DefineMacroArguments) {
   SmallVector<char, 128> Out;
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define MACRO()", Out));
-  EXPECT_STREQ("#define MACRO()", Out.data());
+  EXPECT_EQ("#define MACRO()", rtrim(Out));
 
   ASSERT_FALSE(
       minimizeSourceToDependencyDirectives("#define MACRO(a, b...)", Out));
-  EXPECT_STREQ("#define MACRO(a,b...)\n", Out.data());
+  EXPECT_EQ("#define MACRO(a,b...)", rtrim(Out));
 
   ASSERT_FALSE(
       minimizeSourceToDependencyDirectives("#define MACRO content", Out));
-  EXPECT_STREQ("#define MACRO content", Out.data());
+  EXPECT_EQ("#define MACRO content", rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(
       "#define MACRO   con  tent   ", Out));
-  EXPECT_STREQ("#define MACRO con  tent\n", Out.data());
+  EXPECT_EQ("#define MACRO con  tent", rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(
       "#define MACRO()   con  tent   ", Out));
-  EXPECT_STREQ("#define MACRO() con  tent\n", Out.data());
+  EXPECT_EQ("#define MACRO() con  tent", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, DefineInvalidMacroArguments) {
   SmallVector<char, 128> Out;
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define MACRO((a))", Out));
-  EXPECT_STREQ("#define MACRO\n", Out.data());
+  EXPECT_EQ("#define MACRO", rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define MACRO(", Out));
-  EXPECT_STREQ("#define MACRO\n", Out.data());
+  EXPECT_EQ("#define MACRO", rtrim(Out));
 
   ASSERT_FALSE(
       minimizeSourceToDependencyDirectives("#define MACRO(a * b)", Out));
-  EXPECT_STREQ("#define MACRO\n", Out.data());
+  EXPECT_EQ("#define MACRO", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, DefineHorizontalWhitespace) {
@@ -161,19 +169,19 @@
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(
       "#define MACRO(\t)\tcon \t tent\t", Out));
-  EXPECT_STREQ("#define MACRO() con \t tent\n", Out.data());
+  EXPECT_EQ("#define MACRO() con \t tent", rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(
       "#define MACRO(\f)\fcon \f tent\f", Out));
-  EXPECT_STREQ("#define MACRO() con \f tent\n", Out.data());
+  EXPECT_EQ("#define MACRO() con \f tent", rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(
       "#define MACRO(\v)\vcon \v tent\v", Out));
-  EXPECT_STREQ("#define MACRO() con \v tent\n", Out.data());
+  EXPECT_EQ("#define MACRO() con \v tent", rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(
       "#define MACRO \t\v\f\v\t con\f\t\vtent\v\f \v", Out));
-  EXPECT_STREQ("#define MACRO con\f\t\vtent\n", Out.data());
+  EXPECT_EQ("#define MACRO con\f\t\vtent", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, DefineMultilineArgs) {
@@ -183,7 +191,7 @@
       minimizeSourceToDependencyDirectives("#define MACRO(a        \\\n"
                                            "              )",
                                            Out));
-  EXPECT_STREQ("#define MACRO(a)\n", Out.data());
+  EXPECT_EQ("#define MACRO(a)", rtrim(Out));
 
   ASSERT_FALSE(
       minimizeSourceToDependencyDirectives("#define MACRO(a,       \\\n"
@@ -191,7 +199,7 @@
                                            "        call((a),      \\\n"
                                            "             (b))",
                                            Out));
-  EXPECT_STREQ("#define MACRO(a,b) call((a), (b))\n", Out.data());
+  EXPECT_EQ("#define MACRO(a,b) call((a), (b))", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest,
@@ -204,7 +212,7 @@
                                            "        call((a),      \\\r"
                                            "             (b))",
                                            Out));
-  EXPECT_STREQ("#define MACRO(a,b) call((a), (b))\n", Out.data());
+  EXPECT_EQ("#define MACRO(a,b) call((a), (b))", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, DefineMultilineArgsStringize) {
@@ -214,7 +222,7 @@
                                                     "                #a \\\n"
                                                     "                #b",
                                                     Out));
-  EXPECT_STREQ("#define MACRO(a,b) #a #b\n", Out.data());
+  EXPECT_EQ("#define MACRO(a,b) #a #b", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest,
@@ -227,7 +235,7 @@
                                            "        call((a),      \\\r\n"
                                            "             (b))",
                                            Out));
-  EXPECT_STREQ("#define MACRO(a,b) call((a), (b))\n", Out.data());
+  EXPECT_EQ("#define MACRO(a,b) call((a), (b))", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest,
@@ -240,7 +248,7 @@
                                            "        call((a),      \\\n\r"
                                            "             (b))",
                                            Out));
-  EXPECT_STREQ("#define MACRO(a,b) call((a), (b))\n", Out.data());
+  EXPECT_EQ("#define MACRO(a,b) call((a), (b))", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, DefineNumber) {
@@ -259,12 +267,12 @@
   SmallVector<char, 128> Out;
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define AND&\n", Out));
-  EXPECT_STREQ("#define AND&\n", Out.data());
+  EXPECT_EQ("#define AND&", rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define AND\\\n"
                                                     "&\n",
                                                     Out));
-  EXPECT_STREQ("#define AND &\n", Out.data());
+  EXPECT_EQ("#define AND &", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, MultilineComment) {
@@ -278,9 +286,9 @@
                                            "  /* something */ \n"
                                            "#include  /* \"def\" */ <abc> \n",
                                            Out));
-  EXPECT_STREQ("#define MACRO a\n"
-               "#include <abc>\n",
-               Out.data());
+  EXPECT_EQ("#define MACRO a\n"
+            "#include <abc>",
+            rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, MultilineCommentInStrings) {
@@ -289,9 +297,9 @@
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define MACRO1 \"/*\"\n"
                                                     "#define MACRO2 \"*/\"\n",
                                                     Out));
-  EXPECT_STREQ("#define MACRO1 \"/*\"\n"
-               "#define MACRO2 \"*/\"\n",
-               Out.data());
+  EXPECT_EQ("#define MACRO1 \"/*\"\n"
+            "#define MACRO2 \"*/\"",
+            rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, Ifdef) {
@@ -301,10 +309,10 @@
                                                     "#define B\n"
                                                     "#endif\n",
                                                     Out));
-  EXPECT_STREQ("#ifdef A\n"
-               "#define B\n"
-               "#endif\n",
-               Out.data());
+  EXPECT_EQ("#ifdef A\n"
+            "#define B\n"
+            "#endif",
+            rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("#ifdef A\n"
                                                     "#define B\n"
@@ -316,16 +324,16 @@
                                                     "#define E\n"
                                                     "#endif\n",
                                                     Out));
-  EXPECT_STREQ("#ifdef A\n"
-               "#define B\n"
-               "#elif B\n"
-               "#define C\n"
-               "#elif C\n"
-               "#define D\n"
-               "#else\n"
-               "#define E\n"
-               "#endif\n",
-               Out.data());
+  EXPECT_EQ("#ifdef A\n"
+            "#define B\n"
+            "#elif B\n"
+            "#define C\n"
+            "#elif C\n"
+            "#define D\n"
+            "#else\n"
+            "#define E\n"
+            "#endif",
+            rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, Elifdef) {
@@ -337,12 +345,12 @@
                                                     "#define D\n"
                                                     "#endif\n",
                                                     Out));
-  EXPECT_STREQ("#ifdef A\n"
-               "#define B\n"
-               "#elifdef C\n"
-               "#define D\n"
-               "#endif\n",
-               Out.data());
+  EXPECT_EQ("#ifdef A\n"
+            "#define B\n"
+            "#elifdef C\n"
+            "#define D\n"
+            "#endif",
+            rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("#ifdef A\n"
                                                     "#define B\n"
@@ -354,16 +362,16 @@
                                                     "#define E\n"
                                                     "#endif\n",
                                                     Out));
-  EXPECT_STREQ("#ifdef A\n"
-               "#define B\n"
-               "#elifdef B\n"
-               "#define C\n"
-               "#elifndef C\n"
-               "#define D\n"
-               "#else\n"
-               "#define E\n"
-               "#endif\n",
-               Out.data());
+  EXPECT_EQ("#ifdef A\n"
+            "#define B\n"
+            "#elifdef B\n"
+            "#define C\n"
+            "#elifndef C\n"
+            "#define D\n"
+            "#else\n"
+            "#define E\n"
+            "#endif",
+            rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, EmptyIfdef) {
@@ -376,11 +384,11 @@
                                                     "#else D\n"
                                                     "#endif\n",
                                                     Out));
-  EXPECT_STREQ("#ifdef A\n"
-               "#elif B\n"
-               "#elif C\n"
-               "#endif\n",
-               Out.data());
+  EXPECT_EQ("#ifdef A\n"
+            "#elif B\n"
+            "#elif C\n"
+            "#endif",
+            rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, EmptyElifdef) {
@@ -393,74 +401,74 @@
                                                     "#else D\n"
                                                     "#endif\n",
                                                     Out));
-  EXPECT_STREQ("#ifdef A\n"
-               "#elifdef B\n"
-               "#elifndef C\n"
-               "#endif\n",
-               Out.data());
+  EXPECT_EQ("#ifdef A\n"
+            "#elifdef B\n"
+            "#elifndef C\n"
+            "#endif",
+            rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, Pragma) {
   SmallVector<char, 128> Out;
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("#pragma A\n", Out));
-  EXPECT_STREQ("", Out.data());
+  EXPECT_EQ("", rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("#pragma clang\n", Out));
-  EXPECT_STREQ("", Out.data());
+  EXPECT_EQ("", rtrim(Out));
 
   ASSERT_FALSE(
       minimizeSourceToDependencyDirectives("#pragma clang module\n", Out));
-  EXPECT_STREQ("", Out.data());
+  EXPECT_EQ("", rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(
       "#pragma clang module impor\n", Out));
-  EXPECT_STREQ("", Out.data());
+  EXPECT_EQ("", rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(
       "#pragma clang module import\n", Out));
-  EXPECT_STREQ("#pragma clang module import\n", Out.data());
+  EXPECT_EQ("#pragma clang module import", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, Include) {
   SmallVector<char, 128> Out;
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("#include \"A\"\n", Out));
-  EXPECT_STREQ("#include \"A\"\n", Out.data());
+  EXPECT_EQ("#include \"A\"", rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("#include <A>\n", Out));
-  EXPECT_STREQ("#include <A>\n", Out.data());
+  EXPECT_EQ("#include <A>", rtrim(Out));
 
   ASSERT_FALSE(
       minimizeSourceToDependencyDirectives("#include_next <A>\n", Out));
-  EXPECT_STREQ("#include_next <A>\n", Out.data());
+  EXPECT_EQ("#include_next <A>", rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("#import <A>\n", Out));
-  EXPECT_STREQ("#import <A>\n", Out.data());
+  EXPECT_EQ("#import <A>", rtrim(Out));
 
   ASSERT_FALSE(
       minimizeSourceToDependencyDirectives("#__include_macros <A>\n", Out));
-  EXPECT_STREQ("#__include_macros <A>\n", Out.data());
+  EXPECT_EQ("#__include_macros <A>", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, AtImport) {
   SmallVector<char, 128> Out;
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("@import A;\n", Out));
-  EXPECT_STREQ("@import A;\n", Out.data());
+  EXPECT_EQ("@import A;", rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(" @ import  A;\n", Out));
-  EXPECT_STREQ("@import A;\n", Out.data());
+  EXPECT_EQ("@import A;", rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("@import A\n;", Out));
-  EXPECT_STREQ("@import A;\n", Out.data());
+  EXPECT_EQ("@import A;", rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("@import A.B;\n", Out));
-  EXPECT_STREQ("@import A.B;\n", Out.data());
+  EXPECT_EQ("@import A.B;", rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(
       "@import /*x*/ A /*x*/ . /*x*/ B /*x*/ \n /*x*/ ; /*x*/", Out));
-  EXPECT_STREQ("@import A.B;\n", Out.data());
+  EXPECT_EQ("@import A.B;", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, AtImportFailures) {
@@ -479,10 +487,10 @@
                                                     "R\"()\"\n"
                                                     "#endif\n",
                                                     Out));
-  EXPECT_STREQ("#ifndef GUARD\n"
-               "#define GUARD\n"
-               "#endif\n",
-               Out.data());
+  EXPECT_EQ("#ifndef GUARD\n"
+            "#define GUARD\n"
+            "#endif",
+            rtrim(Out));
 
   bool RawStringLiteralResult = minimizeSourceToDependencyDirectives(
       "#ifndef GUARD\n"
@@ -492,10 +500,10 @@
       "#endif\n",
       Out);
   ASSERT_FALSE(RawStringLiteralResult);
-  EXPECT_STREQ("#ifndef GUARD\n"
-               "#define GUARD\n"
-               "#endif\n",
-               Out.data());
+  EXPECT_EQ("#ifndef GUARD\n"
+            "#define GUARD\n"
+            "#endif",
+            rtrim(Out));
 
   bool RawStringLiteralResult2 = minimizeSourceToDependencyDirectives(
       "#ifndef GUARD\n"
@@ -505,10 +513,10 @@
       "#endif\n",
       Out);
   ASSERT_FALSE(RawStringLiteralResult2);
-  EXPECT_STREQ("#ifndef GUARD\n"
-               "#define GUARD\n"
-               "#endif\n",
-               Out.data());
+  EXPECT_EQ("#ifndef GUARD\n"
+            "#define GUARD\n"
+            "#endif",
+            rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, SplitIdentifier) {
@@ -519,25 +527,25 @@
                                                     "#define GUARD\n"
                                                     "#endif\n",
                                                     Out));
-  EXPECT_STREQ("#ifndef GUARD\n"
-               "#define GUARD\n"
-               "#endif\n",
-               Out.data());
+  EXPECT_EQ("#ifndef GUARD\n"
+            "#define GUARD\n"
+            "#endif",
+            rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define GUA\\\n"
                                                     "RD\n",
                                                     Out));
-  EXPECT_STREQ("#define GUARD\n", Out.data());
+  EXPECT_EQ("#define GUARD", rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define GUA\\\r"
                                                     "RD\n",
                                                     Out));
-  EXPECT_STREQ("#define GUARD\n", Out.data());
+  EXPECT_EQ("#define GUARD", rtrim(Out));
 
   ASSERT_FALSE(minimizeSourceToDependencyDirectives("#define GUA\\\n"
                                                     "           RD\n",
                                                     Out));
-  EXPECT_STREQ("#define GUA RD\n", Out.data());
+  EXPECT_EQ("#define GUA RD", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest,
@@ -548,7 +556,7 @@
                                                     "2 + \\\t\n"
                                                     "3\n",
                                                     Out));
-  EXPECT_STREQ("#define A 1 + 2 + 3\n", Out.data());
+  EXPECT_EQ("#define A 1 + 2 + 3", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, PoundWarningAndError) {
@@ -565,7 +573,7 @@
            "#error \\\n#include <t.h>\n#include <t.h>\n",
        }) {
     ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
-    EXPECT_STREQ("#include <t.h>\n", Out.data());
+    EXPECT_EQ("#include <t.h>", rtrim(Out));
   }
 
   for (auto Source : {
@@ -573,7 +581,7 @@
            "#error \\\n#include <t.h>\n",
        }) {
     ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
-    EXPECT_STREQ("", Out.data());
+    EXPECT_EQ("", rtrim(Out));
   }
 
   for (auto Source : {
@@ -585,7 +593,7 @@
            "#if MACRO\n#error /*\n#endif\n",
        }) {
     ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
-    EXPECT_STREQ("#if MACRO\n#endif\n", Out.data());
+    EXPECT_EQ("#if MACRO\n#endif", rtrim(Out));
   }
 }
 
@@ -600,7 +608,7 @@
 #include <foo>
 )";
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
-  EXPECT_STREQ("#include <bob>\n#include <foo>\n", Out.data());
+  EXPECT_EQ("#include <bob>\n#include <foo>", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, CharacterLiteralPrefixL) {
@@ -613,7 +621,7 @@
 #include <test.h>
 )";
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
-  EXPECT_STREQ("#if DEBUG\n#endif\n#include <test.h>\n", Out.data());
+  EXPECT_EQ("#if DEBUG\n#endif\n#include <test.h>", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, CharacterLiteralPrefixU) {
@@ -624,7 +632,7 @@
 // '
 )";
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
-  EXPECT_STREQ("#include <test.h>\n", Out.data());
+  EXPECT_EQ("#include <test.h>", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, CharacterLiteralPrefixu) {
@@ -637,7 +645,7 @@
 // '
 )";
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
-  EXPECT_STREQ("#include <test.h>\n", Out.data());
+  EXPECT_EQ("#include <test.h>", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, PragmaOnce) {
@@ -650,7 +658,7 @@
 #include <test.h>
 )";
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out, Tokens));
-  EXPECT_STREQ("#pragma once\n#include <test.h>\n", Out.data());
+  EXPECT_EQ("#pragma once\n#include <test.h>", rtrim(Out));
   ASSERT_EQ(Tokens.size(), 3u);
   EXPECT_EQ(Tokens[0].K,
             minimize_source_to_dependency_directives::pp_pragma_once);
@@ -661,7 +669,7 @@
     #include <test.h>
     )";
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
-  EXPECT_STREQ("#pragma once\n#include <test.h>\n", Out.data());
+  EXPECT_EQ("#pragma once\n#include <test.h>", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest,
@@ -675,9 +683,9 @@
     void foo();
 )";
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
-  EXPECT_STREQ(
-      "#if NEVER_ENABLED\n#define why(fmt,...) #error don't try me\n#endif\n",
-      Out.data());
+  EXPECT_EQ(
+      "#if NEVER_ENABLED\n#define why(fmt,...) #error don't try me\n#endif",
+      rtrim(Out));
 
   Source = R"(#if NEVER_ENABLED
       #define why(fmt, ...) "quote dropped
@@ -686,9 +694,8 @@
       void foo();
   )";
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
-  EXPECT_STREQ(
-      "#if NEVER_ENABLED\n#define why(fmt,...) \"quote dropped\n#endif\n",
-      Out.data());
+  EXPECT_EQ("#if NEVER_ENABLED\n#define why(fmt,...) \"quote dropped\n#endif",
+            rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest,
@@ -697,15 +704,15 @@
 
   StringRef Source = "#define X '\\ \t\nx'\nvoid foo() {}";
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
-  EXPECT_STREQ("#define X '\\ \t\nx'\n", Out.data());
+  EXPECT_EQ("#define X '\\ \t\nx'", rtrim(Out));
 
   Source = "#define X \"\\ \r\nx\"\nvoid foo() {}";
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
-  EXPECT_STREQ("#define X \"\\ \r\nx\"\n", Out.data());
+  EXPECT_EQ("#define X \"\\ \r\nx\"", rtrim(Out));
 
   Source = "#define X \"\\ \r\nx\n#include <x>\n";
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out));
-  EXPECT_STREQ("#define X \"\\ \r\nx\n#include <x>\n", Out.data());
+  EXPECT_EQ("#define X \"\\ \r\nx\n#include <x>", rtrim(Out));
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, CxxModules) {
@@ -740,11 +747,12 @@
     }
     )";
   ASSERT_FALSE(minimizeSourceToDependencyDirectives(Source, Out, Tokens));
-  EXPECT_STREQ("#include \"textual-header.h\"\nexport module m;\n"
-               "export import :l [[rename]];\n"
-               "import <<= 3;\nimport a b d e d e f e;\n"
-               "import foo [[no_unique_address]];\nimport foo();\n"
-               "import f(:sefse);\nimport f(->a = 3);\n", Out.data());
+  EXPECT_EQ("#include \"textual-header.h\"\nexport module m;\n"
+            "export import :l [[rename]];\n"
+            "import <<= 3;\nimport a b d e d e f e;\n"
+            "import foo [[no_unique_address]];\nimport foo();\n"
+            "import f(:sefse);\nimport f(->a = 3);",
+            rtrim(Out));
   ASSERT_EQ(Tokens.size(), 12u);
   EXPECT_EQ(Tokens[0].K,
             minimize_source_to_dependency_directives::pp_include);
Index: clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp
===================================================================
--- clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp
+++ clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp
@@ -36,9 +36,10 @@
 
   Minimizer(SmallVectorImpl<char> &Out, SmallVectorImpl<Token> &Tokens,
             StringRef Input, DiagnosticsEngine *Diags,
-            SourceLocation InputSourceLoc)
+            SourceLocation InputSourceLoc, bool PadOutputToInputSize)
       : Out(Out), Tokens(Tokens), Input(Input), Diags(Diags),
-        InputSourceLoc(InputSourceLoc) {}
+        InputSourceLoc(InputSourceLoc),
+        PadOutputToInputSize(PadOutputToInputSize) {}
 
   /// Lex the provided source and emit the minimized output.
   ///
@@ -103,6 +104,8 @@
   StringRef Input;
   DiagnosticsEngine *Diags;
   SourceLocation InputSourceLoc;
+  /// Whether the output should be padded to match the size of the input.
+  bool PadOutputToInputSize;
 };
 
 } // end anonymous namespace
@@ -900,12 +903,20 @@
 bool Minimizer::minimize() {
   bool Error = minimizeImpl(Input.begin(), Input.end());
 
-  assert(Out.size() <= Input.size() && "Output is smaller than input");
+  assert(Out.size() <= Input.size() && "Output is not larger than input");
 
   if (!Error) {
-    // Add a trailing newline if it won't make the output larger than the input.
-    if (!Out.empty() && Out.back() != '\n' && Out.size() < Input.size())
+    // Always try to ensure the contents ends with newline.
+    if (Out.size() < Input.size())
+      if (!Out.empty() && Out.back() != '\n')
+        Out.push_back('\n');
+
+    // Pad the output with whitespace if asked to do so.
+    if (PadOutputToInputSize && Out.size() < Input.size()) {
+      Out.insert(Out.end(), Input.size() - Out.size() - 1, ' ');
       Out.push_back('\n');
+    }
+
     makeToken(pp_eof);
   }
 
@@ -966,11 +977,15 @@
   return false;
 }
 
-bool clang::minimizeSourceToDependencyDirectives(
-    StringRef Input, SmallVectorImpl<char> &Output,
-    SmallVectorImpl<Token> &Tokens, DiagnosticsEngine *Diags,
-    SourceLocation InputSourceLoc) {
+bool clang::minimizeSourceToDependencyDirectives(StringRef Input,
+                                                 SmallVectorImpl<char> &Output,
+                                                 SmallVectorImpl<Token> &Tokens,
+                                                 DiagnosticsEngine *Diags,
+                                                 SourceLocation InputSourceLoc,
+                                                 bool PadOutputToInputSize) {
   Output.clear();
   Tokens.clear();
-  return Minimizer(Output, Tokens, Input, Diags, InputSourceLoc).minimize();
+  return Minimizer(Output, Tokens, Input, Diags, InputSourceLoc,
+                   PadOutputToInputSize)
+      .minimize();
 }
Index: clang/include/clang/Lex/DependencyDirectivesSourceMinimizer.h
===================================================================
--- clang/include/clang/Lex/DependencyDirectivesSourceMinimizer.h
+++ clang/include/clang/Lex/DependencyDirectivesSourceMinimizer.h
@@ -105,7 +105,8 @@
     llvm::SmallVectorImpl<minimize_source_to_dependency_directives::Token>
         &Tokens,
     DiagnosticsEngine *Diags = nullptr,
-    SourceLocation InputSourceLoc = SourceLocation());
+    SourceLocation InputSourceLoc = SourceLocation(),
+    bool PadOutputToInputSize = false);
 
 } // end namespace clang
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D104462: [... Jan Svoboda via Phabricator via cfe-commits

Reply via email to