r369993 - [clang-scan-deps] Skip UTF-8 BOM in source minimizer

Alexandre Ganea via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 26 17:13:52 PDT 2019


Author: aganea
Date: Mon Aug 26 17:13:52 2019
New Revision: 369993

URL: http://llvm.org/viewvc/llvm-project?rev=369993&view=rev
Log:
[clang-scan-deps] Skip UTF-8 BOM in source minimizer

Differential Revision: https://reviews.llvm.org/D66511

Added:
    cfe/trunk/test/Lexer/minimize_source_to_dependency_directives_utf8bom.c
Modified:
    cfe/trunk/lib/Lex/DependencyDirectivesSourceMinimizer.cpp

Modified: cfe/trunk/lib/Lex/DependencyDirectivesSourceMinimizer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/DependencyDirectivesSourceMinimizer.cpp?rev=369993&r1=369992&r2=369993&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/DependencyDirectivesSourceMinimizer.cpp (original)
+++ cfe/trunk/lib/Lex/DependencyDirectivesSourceMinimizer.cpp Mon Aug 26 17:13:52 2019
@@ -834,7 +834,14 @@ bool Minimizer::lexPPLine(const char *&F
   return lexDefault(Kind, Id.Name, First, End);
 }
 
+static void skipUTF8ByteOrderMark(const char *&First, const char *const End) {
+  if ((End - First) >= 3 && First[0] == '\xef' && First[1] == '\xbb' &&
+      First[2] == '\xbf')
+    First += 3;
+}
+
 bool Minimizer::minimizeImpl(const char *First, const char *const End) {
+  skipUTF8ByteOrderMark(First, End);
   while (First != End)
     if (lexPPLine(First, End))
       return true;

Added: cfe/trunk/test/Lexer/minimize_source_to_dependency_directives_utf8bom.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/minimize_source_to_dependency_directives_utf8bom.c?rev=369993&view=auto
==============================================================================
--- cfe/trunk/test/Lexer/minimize_source_to_dependency_directives_utf8bom.c (added)
+++ cfe/trunk/test/Lexer/minimize_source_to_dependency_directives_utf8bom.c Mon Aug 26 17:13:52 2019
@@ -0,0 +1,10 @@
+// Test UTF8 BOM at start of file
+// RUN: printf '\xef\xbb\xbf' > %t.c
+// RUN: echo '#ifdef TEST\n' >> %t.c
+// RUN: echo '#include <string>' >> %t.c
+// RUN: echo '#endif' >> %t.c
+// RUN: %clang_cc1 -DTEST -print-dependency-directives-minimized-source %t.c 2>&1 | FileCheck %s
+
+// CHECK:      #ifdef TEST
+// CHECK-NEXT: #include <string>
+// CHECK-NEXT: #endif




More information about the cfe-commits mailing list