<div dir="ltr"><div dir="ltr">On Mon, 26 Aug 2019 at 16:17, Alexandre Ganea via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Author: aganea<br>
Date: Mon Aug 26 16:19:21 2019<br>
New Revision: 369986<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=369986&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=369986&view=rev</a><br>
Log:<br>
[clang-scan-deps] Minimizer: Correctly handle multi-line content with CR+LF line endings<br>
<br>
Previously, an #error directive with quoted, multi-line content, along with CR+LF line endings wasn't handled correctly.<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D66556" rel="noreferrer" target="_blank">https://reviews.llvm.org/D66556</a><br>
<br>
Added:<br>
cfe/trunk/test/Lexer/minimize_source_to_dependency_directives_invalid_error.c<br>
Modified:<br>
cfe/trunk/lib/Lex/DependencyDirectivesSourceMinimizer.cpp<br>
<br>
Modified: cfe/trunk/lib/Lex/DependencyDirectivesSourceMinimizer.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/DependencyDirectivesSourceMinimizer.cpp?rev=369986&r1=369985&r2=369986&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/DependencyDirectivesSourceMinimizer.cpp?rev=369986&r1=369985&r2=369986&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Lex/DependencyDirectivesSourceMinimizer.cpp (original)<br>
+++ cfe/trunk/lib/Lex/DependencyDirectivesSourceMinimizer.cpp Mon Aug 26 16:19:21 2019<br>
@@ -196,15 +196,29 @@ static void skipString(const char *&Firs<br>
++First; // Finish off the string.<br>
}<br>
<br>
-static void skipNewline(const char *&First, const char *End) {<br>
- assert(isVerticalWhitespace(*First));<br>
- ++First;<br>
+// Returns the length of EOL, either 0 (no end-of-line), 1 (\n) or 2 (\r\n)<br>
+static unsigned isEOL(const char *First, const char *const End) {<br>
if (First == End)<br>
- return;<br>
+ return 0;<br>
+ if (End - First > 1 && isVerticalWhitespace(First[0]) &&<br>
+ isVerticalWhitespace(First[1]) && First[0] != First[1])<br>
+ return 2;<br>
+ return !!isVerticalWhitespace(First[0]);<br>
+}<br>
+<br>
+// Returns the length of the skipped newline<br>
+static unsigned skipNewline(const char *&First, const char *End) {<br>
+ if (First == End)<br>
+ return 0;<br>
+ assert(isVerticalWhitespace(*First));<br>
+ unsigned Len = isEOL(First, End);<br>
+ assert(Len && "expected newline");<br>
+ First += Len;<br>
+ return Len;<br>
+}<br>
<br>
- // Check for "\n\r" and "\r\n".<br>
- if (LLVM_UNLIKELY(isVerticalWhitespace(*First) && First[-1] != First[0]))<br>
- ++First;<br>
+static bool wasLineContinuation(const char *First, unsigned EOLLen) {<br>
+ return *(First - (int)EOLLen - 1) == '\\';<br>
}<br>
<br>
static void skipToNewlineRaw(const char *&First, const char *const End) {<br>
@@ -212,17 +226,21 @@ static void skipToNewlineRaw(const char<br>
if (First == End)<br>
return;<br>
<br>
- if (isVerticalWhitespace(*First))<br>
+ unsigned Len = isEOL(First, End);<br>
+ if (Len)<br>
return;<br>
<br>
- while (!isVerticalWhitespace(*First))<br>
+ do {<br>
if (++First == End)<br>
return;<br>
+ Len = isEOL(First, End);<br>
+ } while (!Len);<br>
<br>
if (First[-1] != '\\')<br>
return;<br>
<br>
- ++First; // Keep going...<br>
+ First += Len;<br>
+ // Keep skipping lines...<br>
}<br>
}<br>
<br>
@@ -277,7 +295,7 @@ static bool isQuoteCppDigitSeparator(con<br>
}<br>
<br>
static void skipLine(const char *&First, const char *const End) {<br>
- do {<br>
+ for (;;) {<br>
assert(First <= End);<br>
if (First == End)<br>
return;<br>
@@ -322,9 +340,10 @@ static void skipLine(const char *&First,<br>
return;<br>
<br>
// Skip over the newline.<br>
- assert(isVerticalWhitespace(*First));<br>
- skipNewline(First, End);<br>
- } while (First[-2] == '\\'); // Continue past line-continuations.<br>
+ unsigned Len = skipNewline(First, End);<br>
+ if (!wasLineContinuation(First, Len)) // Continue past line-continuations.<br>
+ break;<br>
+ }<br>
}<br>
<br>
static void skipDirective(StringRef Name, const char *&First,<br>
@@ -379,6 +398,8 @@ void Minimizer::printToNewline(const cha<br>
// Print out the string.<br>
if (Last == End || Last == First || Last[-1] != '\\') {<br>
append(First, reverseOverSpaces(First, Last));<br>
+ First = Last;<br>
+ skipNewline(First, End);<br>
return;<br>
}<br>
<br>
<br>
Added: cfe/trunk/test/Lexer/minimize_source_to_dependency_directives_invalid_error.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/minimize_source_to_dependency_directives_invalid_error.c?rev=369986&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/minimize_source_to_dependency_directives_invalid_error.c?rev=369986&view=auto</a><br>
==============================================================================<br>
--- cfe/trunk/test/Lexer/minimize_source_to_dependency_directives_invalid_error.c (added)<br>
+++ cfe/trunk/test/Lexer/minimize_source_to_dependency_directives_invalid_error.c Mon Aug 26 16:19:21 2019<br>
@@ -0,0 +1,16 @@<br>
+// Test CF+LF are properly handled along with quoted, multi-line #error<br>
+// RUN: cat %s | unix2dos | %clang_cc1 -DOTHER -print-dependency-directives-minimized-source 2>&1 | FileCheck %s<br></blockquote><div><br></div><div>You can't rely on 'unix2dos' existing across all our supported build environments; this is causing a test failure for me.</div><div><br></div><div>I'm reverting for now. Maybe you could just check in a file with CRLF line endings for this test (marked as binary rather than text so that clients don't "fix" it)?</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+<br>
+#ifndef TEST<br>
+#error "message \<br>
+ more message \<br>
+ even more"<br>
+#endif<br>
+<br>
+#ifdef OTHER<br>
+#include <string><br>
+#endif<br>
+<br>
+// CHECK: #ifdef OTHER<br>
+// CHECK-NEXT: #include <string><br>
+// CHECK-NEXT: #endif<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
</blockquote></div></div>