From 5c461df836445bf7a4639c1145abaf3ecc6cd432 Mon Sep 17 00:00:00 2001
From: Lubos Lunak <l.lunak@suse.cz>
Date: Wed, 27 Nov 2013 21:14:43 +0000
Subject: [PATCH] strip UTF-8 BOM in -frewrite-includes (PR#15664)

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@195877 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/Rewrite/Frontend/InclusionRewriter.cpp  | 5 +++++
 test/Frontend/Inputs/rewrite-includes-bom.h | 1 +
 test/Frontend/rewrite-includes-bom.c        | 4 ++++
 3 files changed, 10 insertions(+)
 create mode 100644 test/Frontend/Inputs/rewrite-includes-bom.h
 create mode 100644 test/Frontend/rewrite-includes-bom.c

diff --git a/lib/Rewrite/Frontend/InclusionRewriter.cpp b/lib/Rewrite/Frontend/InclusionRewriter.cpp
index bd4250adaad..71ceb5f0b6a 100644
--- a/lib/Rewrite/Frontend/InclusionRewriter.cpp
+++ b/lib/Rewrite/Frontend/InclusionRewriter.cpp
@@ -367,6 +367,11 @@ bool InclusionRewriter::Process(FileID FileId,
   unsigned NextToWrite = 0;
   int Line = 1; // The current input file line number.
 
+  // Ignore UTF-8 BOM, otherwise it'd end up somewhere else than the start
+  // of the resulting file.
+  if (FromFile.getBuffer().startswith("\xEF\xBB\xBF"))
+    NextToWrite = 3;
+
   Token RawToken;
   RawLex.LexFromRawLexer(RawToken);
 
diff --git a/test/Frontend/Inputs/rewrite-includes-bom.h b/test/Frontend/Inputs/rewrite-includes-bom.h
new file mode 100644
index 00000000000..7ba011fc2d7
--- /dev/null
+++ b/test/Frontend/Inputs/rewrite-includes-bom.h
@@ -0,0 +1 @@
+// This file starts with UTF-8 BOM marker.
diff --git a/test/Frontend/rewrite-includes-bom.c b/test/Frontend/rewrite-includes-bom.c
new file mode 100644
index 00000000000..a1aa4c98f86
--- /dev/null
+++ b/test/Frontend/rewrite-includes-bom.c
@@ -0,0 +1,4 @@
+// RUN: %clang -E -frewrite-includes -I %S/Inputs %s -o - | %clang -fsyntax-only -Xclang -verify -x c -
+// expected-no-diagnostics
+
+#include "rewrite-includes-bom.h"
-- 
GitLab