<div dir="auto">Wouldn't a new sublib below tooling fix the deposit cycle as well?</div><br><div class="gmail_quote"><div dir="ltr">On Wed, May 16, 2018, 2:05 PM Eric Liu <<a href="mailto:ioeric@google.com">ioeric@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Nico,<div><br></div><div>I replied on <a href="https://reviews.llvm.org/rL332284" target="_blank" rel="noreferrer">https://reviews.llvm.org/rL332284</a>. Here is a copy:</div><div><div class="m_-6894102575281797154inbox-inbox-phui-timeline-shell" id="m_-6894102575281797154inbox-inbox-anchor-251344" style="margin:0px;padding:0px;border:0px;font-family:"Segoe UI","Segoe UI Emoji","Segoe UI Symbol",Lato,"Helvetica Neue",Helvetica,Arial,sans-serif;background-color:rgb(243,245,247)"><div class="m_-6894102575281797154inbox-inbox-phui-timeline-event-view m_-6894102575281797154inbox-inbox-phui-timeline-major-event" style="margin:0px 0px 0px 62px;padding:0px;border:0px"><div class="m_-6894102575281797154inbox-inbox-phui-timeline-content" style="margin:0px;padding:0px;border-width:1px 0px;border-top-style:solid;border-right-style:initial;border-bottom-style:solid;border-left-style:initial;border-top-color:rgb(213,216,225);border-right-color:initial;border-bottom-color:rgb(213,216,225);border-left-color:initial;border-radius:3px"><div class="m_-6894102575281797154inbox-inbox-phui-timeline-group m_-6894102575281797154inbox-inbox-phui-timeline-border" style="margin:0px;padding:0px;border-width:0px 1px;border-top-style:initial;border-right-style:solid;border-bottom-style:initial;border-left-style:solid;border-top-color:initial;border-right-color:rgb(213,216,225);border-bottom-color:initial;border-left-color:rgb(213,216,225);border-radius:3px"><div class="m_-6894102575281797154inbox-inbox-phui-timeline-inner-content" style="margin:0px;padding:0px;border:0px"><div class="m_-6894102575281797154inbox-inbox-phui-timeline-core-content" style="margin:0px;padding:16px;border-width:1px 0px 0px;border-top-style:solid;border-right-style:initial;border-bottom-style:initial;border-left-style:initial;border-top-color:rgba(71,87,120,0.1);border-right-color:initial;border-bottom-color:initial;border-left-color:initial;overflow-x:auto;line-height:18px;background:rgb(255,255,255);border-bottom-left-radius:3px;border-bottom-right-radius:3px"><span class="m_-6894102575281797154inbox-inbox-transaction-comment"><div class="m_-6894102575281797154inbox-inbox-phabricator-remarkup" style="margin:0px;padding:0px;border:0px;line-height:1.7em;word-break:break-word"><p style="margin:0px;padding:0px;border:0px">This patch is pre-work for <a href="https://reviews.llvm.org/rL332287" class="m_-6894102575281797154inbox-inbox-phui-tag-view m_-6894102575281797154inbox-inbox-phui-tag-type-object" style="text-decoration-line:none;color:rgb(0,0,0);font-weight:bold;white-space:nowrap;margin-top:0px" target="_blank" rel="noreferrer"><span class="m_-6894102575281797154inbox-inbox-phui-tag-core m_-6894102575281797154inbox-inbox-phui-tag-color-object" style="color:inherit;border:1px solid rgb(199,204,217);border-radius:3px;padding:0px 4px;background-color:rgb(235,236,238);margin-top:0px">rL332287</span></a>, which adds header insertion functionality into libToolingCore. The main reason why we put them here (instead of libTooling) is to avoid cyclic dependencies with clang-format (libTooling -> clang-format -> libTooling), and I thought this should be fine given that the library is text-based and fairly light-weight. And I also think include insertion is a functionality that even clang could potentially make use of e.g. FixIt that adds new #include in a diagnostic (I recall there were interests in this).</p></div></span></div></div></div></div></div></div><br class="m_-6894102575281797154inbox-inbox-Apple-interchange-newline"></div><div>Let me know if you have any concern.</div><div><br></div><div>Thanks,</div><div>Eric</div></div><br><div class="gmail_quote"><div dir="ltr">On Wed, May 16, 2018 at 7:50 PM Nico Weber <<a href="mailto:thakis@chromium.org" target="_blank" rel="noreferrer">thakis@chromium.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Eric,<div><br></div><div>I thought I said this on some phab issue, but I can't find it right now -- apologies if you've answered this elsewhere already: libToolingCore is linked into many places, e.g. clang. For that reason, It doesn't feel like a great fit for all this formatting-related stuff -- maybe there should be a new subdir of lib/Tooling for this instead?</div></div><div class="gmail_extra"><br><div class="gmail_quote"></div></div><div class="gmail_extra"><div class="gmail_quote">On Mon, May 14, 2018 at 4:17 PM, Eric Liu via cfe-commits <span dir="ltr"><<a href="mailto:cfe-commits@lists.llvm.org" target="_blank" rel="noreferrer">cfe-commits@lists.llvm.org</a>></span> wrote:<br></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: ioeric<br>
Date: Mon May 14 13:17:53 2018<br>
New Revision: 332287<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=332287&view=rev" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=332287&view=rev</a><br>
Log:<br>
[Tooling] Pull #include manipulation code from clangFormat into libToolingCore.<br>
<br>
Summary: Also pull #include related style out of FormatStyle as tooling::IncludeStyle.<br>
<br>
Reviewers: ilya-biryukov<br>
<br>
Reviewed By: ilya-biryukov<br>
<br>
Subscribers: klimek, mgorny, cfe-commits, djasper<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D46496" rel="noreferrer noreferrer" target="_blank">https://reviews.llvm.org/D46496</a><br>
<br>
Added:<br>
    cfe/trunk/include/clang/Tooling/Core/HeaderIncludes.h<br>
    cfe/trunk/lib/Tooling/Core/HeaderIncludes.cpp<br>
    cfe/trunk/unittests/Tooling/HeaderIncludesTest.cpp<br>
Modified:<br>
    cfe/trunk/include/clang/Format/Format.h<br>
    cfe/trunk/lib/Format/Format.cpp<br>
    cfe/trunk/lib/Tooling/Core/CMakeLists.txt<br>
    cfe/trunk/unittests/Format/CleanupTest.cpp<br>
    cfe/trunk/unittests/Tooling/CMakeLists.txt<br>
<br>
Modified: cfe/trunk/include/clang/Format/Format.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=332287&r1=332286&r2=332287&view=diff" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=332287&r1=332286&r2=332287&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/include/clang/Format/Format.h (original)<br>
+++ cfe/trunk/include/clang/Format/Format.h Mon May 14 13:17:53 2018<br>
@@ -19,6 +19,7 @@<br>
 #include "clang/Tooling/Core/IncludeStyle.h"<br>
 #include "clang/Tooling/Core/Replacement.h"<br>
 #include "llvm/ADT/ArrayRef.h"<br>
+#include "llvm/Support/Regex.h"<br>
 #include <system_error><br>
<br>
 namespace clang {<br>
@@ -1810,17 +1811,13 @@ formatReplacements(StringRef Code, const<br>
 /// This also supports inserting/deleting C++ #include directives:<br>
 /// - If a replacement has offset UINT_MAX, length 0, and a replacement text<br>
 ///   that is an #include directive, this will insert the #include into the<br>
-///   correct block in the \p Code. When searching for points to insert new<br>
-///   header, this ignores #include's after the #include block(s) in the<br>
-///   beginning of a file to avoid inserting headers into code sections where<br>
-///   new #include's should not be added by default. These code sections<br>
-///   include:<br>
-///     - raw string literals (containing #include).<br>
-///     - #if blocks.<br>
-///     - Special #include's among declarations (e.g. functions).<br>
+///   correct block in the \p Code.<br>
 /// - If a replacement has offset UINT_MAX, length 1, and a replacement text<br>
 ///   that is the name of the header to be removed, the header will be removed<br>
 ///   from \p Code if it exists.<br>
+/// The include manipulation is done via `tooling::HeaderInclude`, see its<br>
+/// documentation for more details on how include insertion points are found and<br>
+/// what edits are produced.<br>
 llvm::Expected<tooling::Replacements><br>
 cleanupAroundReplacements(StringRef Code, const tooling::Replacements &Replaces,<br>
                           const FormatStyle &Style);<br>
<br>
Added: cfe/trunk/include/clang/Tooling/Core/HeaderIncludes.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Core/HeaderIncludes.h?rev=332287&view=auto" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Core/HeaderIncludes.h?rev=332287&view=auto</a><br>
==============================================================================<br>
--- cfe/trunk/include/clang/Tooling/Core/HeaderIncludes.h (added)<br>
+++ cfe/trunk/include/clang/Tooling/Core/HeaderIncludes.h Mon May 14 13:17:53 2018<br>
@@ -0,0 +1,137 @@<br>
+//===--- HeaderIncludes.h - Insert/Delete #includes for C++ code--*- C++-*-===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#ifndef LLVM_CLANG_TOOLING_CORE_HEADERINCLUDES_H<br>
+#define LLVM_CLANG_TOOLING_CORE_HEADERINCLUDES_H<br>
+<br>
+#include "clang/Basic/SourceManager.h"<br>
+#include "clang/Tooling/Core/Replacement.h"<br>
+#include "clang/Tooling/Core/IncludeStyle.h"<br>
+#include "llvm/Support/Path.h"<br>
+#include "llvm/Support/Regex.h"<br>
+#include <unordered_map><br>
+<br>
+namespace clang {<br>
+namespace tooling {<br>
+<br>
+/// This class manages priorities of C++ #include categories and calculates<br>
+/// priorities for headers.<br>
+/// FIXME(ioeric): move this class into implementation file when clang-format's<br>
+/// include sorting functions are also moved here.<br>
+class IncludeCategoryManager {<br>
+public:<br>
+  IncludeCategoryManager(const IncludeStyle &Style, StringRef FileName);<br>
+<br>
+  /// Returns the priority of the category which \p IncludeName belongs to.<br>
+  /// If \p CheckMainHeader is true and \p IncludeName is a main header, returns<br>
+  /// 0. Otherwise, returns the priority of the matching category or INT_MAX.<br>
+  /// NOTE: this API is not thread-safe!<br>
+  int getIncludePriority(StringRef IncludeName, bool CheckMainHeader) const;<br>
+<br>
+private:<br>
+  bool isMainHeader(StringRef IncludeName) const;<br>
+<br>
+  const IncludeStyle Style;<br>
+  bool IsMainFile;<br>
+  std::string FileName;<br>
+  // This refers to a substring in FileName.<br>
+  StringRef FileStem;<br>
+  // Regex is not thread-safe.<br>
+  mutable SmallVector<llvm::Regex, 4> CategoryRegexs;<br>
+};<br>
+<br>
+/// Generates replacements for inserting or deleting #include directives in a<br>
+/// file.<br>
+class HeaderIncludes {<br>
+public:<br>
+  HeaderIncludes(llvm::StringRef FileName, llvm::StringRef Code,<br>
+                 const IncludeStyle &Style);<br>
+<br>
+  /// Inserts an #include directive of \p Header into the code. If \p IsAngled<br>
+  /// is true, \p Header will be quoted with <> in the directive; otherwise, it<br>
+  /// will be quoted with "".<br>
+  ///<br>
+  /// When searching for points to insert new header, this ignores #include's<br>
+  /// after the #include block(s) in the beginning of a file to avoid inserting<br>
+  /// headers into code sections where new #include's should not be added by<br>
+  /// default. These code sections include:<br>
+  ///   - raw string literals (containing #include).<br>
+  ///   - #if blocks.<br>
+  ///   - Special #include's among declarations (e.g. functions).<br>
+  ///<br>
+  /// Returns a replacement that inserts the new header into a suitable #include<br>
+  /// block of the same category. This respects the order of the existing<br>
+  /// #includes in the block; if the existing #includes are not already sorted,<br>
+  /// this will simply insert the #include in front of the first #include of the<br>
+  /// same category in the code that should be sorted after \p IncludeName. If<br>
+  /// \p IncludeName already exists (with exactly the same spelling), this<br>
+  /// returns None.<br>
+  llvm::Optional<tooling::Replacement> insert(llvm::StringRef Header,<br>
+                                              bool IsAngled) const;<br>
+<br>
+  /// Removes all existing #includes of \p Header quoted with <> if \p IsAngled<br>
+  /// is true or "" if \p IsAngled is false.<br>
+  /// This doesn't resolve the header file path; it only deletes #includes with<br>
+  /// exactly the same spelling.<br>
+  tooling::Replacements remove(llvm::StringRef Header, bool IsAngled) const;<br>
+<br>
+private:<br>
+  struct Include {<br>
+    Include(StringRef Name, tooling::Range R) : Name(Name), R(R) {}<br>
+<br>
+    // An include header quoted with either <> or "".<br>
+    std::string Name;<br>
+    // The range of the whole line of include directive including any eading<br>
+    // whitespaces and trailing comment.<br>
+    tooling::Range R;<br>
+  };<br>
+<br>
+  void addExistingInclude(Include IncludeToAdd, unsigned NextLineOffset);<br>
+<br>
+  std::string FileName;<br>
+  std::string Code;<br>
+<br>
+  // Map from include name (quotation trimmed) to a list of existing includes<br>
+  // (in case there are more than one) with the name in the current file. <x><br>
+  // and "x" will be treated as the same header when deleting #includes.<br>
+  llvm::StringMap<llvm::SmallVector<Include, 1>> ExistingIncludes;<br>
+<br>
+  /// Map from priorities of #include categories to all #includes in the same<br>
+  /// category. This is used to find #includes of the same category when<br>
+  /// inserting new #includes. #includes in the same categories are sorted in<br>
+  /// in the order they appear in the source file.<br>
+  /// See comment for "FormatStyle::IncludeCategories" for details about include<br>
+  /// priorities.<br>
+  std::unordered_map<int, llvm::SmallVector<const Include *, 8>><br>
+      IncludesByPriority;<br>
+<br>
+  int FirstIncludeOffset;<br>
+  // All new headers should be inserted after this offset (e.g. after header<br>
+  // guards, file comment).<br>
+  unsigned MinInsertOffset;<br>
+  // Max insertion offset in the original code. For example, we want to avoid<br>
+  // inserting new #includes into the actual code section (e.g. after a<br>
+  // declaration).<br>
+  unsigned MaxInsertOffset;<br>
+  IncludeCategoryManager Categories;<br>
+  // Record the offset of the end of the last include in each category.<br>
+  std::unordered_map<int, int> CategoryEndOffsets;<br>
+<br>
+  // All possible priorities.<br>
+  std::set<int> Priorities;<br>
+<br>
+  // Matches a whole #include directive.<br>
+  llvm::Regex IncludeRegex;<br>
+};<br>
+<br>
+<br>
+} // namespace tooling<br>
+} // namespace clang<br>
+<br>
+#endif // LLVM_CLANG_TOOLING_CORE_HEADERINCLUDES_H<br>
<br>
Modified: cfe/trunk/lib/Format/Format.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=332287&r1=332286&r2=332287&view=diff" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=332287&r1=332286&r2=332287&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Format/Format.cpp (original)<br>
+++ cfe/trunk/lib/Format/Format.cpp Mon May 14 13:17:53 2018<br>
@@ -31,6 +31,7 @@<br>
 #include "clang/Basic/SourceManager.h"<br>
 #include "clang/Basic/VirtualFileSystem.h"<br>
 #include "clang/Lex/Lexer.h"<br>
+#include "clang/Tooling/Core/HeaderIncludes.h"<br>
 #include "llvm/ADT/STLExtras.h"<br>
 #include "llvm/ADT/StringRef.h"<br>
 #include "llvm/Support/Allocator.h"<br>
@@ -1676,62 +1677,6 @@ static void sortCppIncludes(const Format<br>
<br>
 namespace {<br>
<br>
-// This class manages priorities of #include categories and calculates<br>
-// priorities for headers.<br>
-class IncludeCategoryManager {<br>
-public:<br>
-  IncludeCategoryManager(const FormatStyle &Style, StringRef FileName)<br>
-      : Style(Style), FileName(FileName) {<br>
-    FileStem = llvm::sys::path::stem(FileName);<br>
-    for (const auto &Category : Style.IncludeStyle.IncludeCategories)<br>
-      CategoryRegexs.emplace_back(Category.Regex, llvm::Regex::IgnoreCase);<br>
-    IsMainFile = FileName.endswith(".c") || FileName.endswith(".cc") ||<br>
-                 FileName.endswith(".cpp") || FileName.endswith(".c++") ||<br>
-                 FileName.endswith(".cxx") || FileName.endswith(".m") ||<br>
-                 FileName.endswith(".mm");<br>
-  }<br>
-<br>
-  // Returns the priority of the category which \p IncludeName belongs to.<br>
-  // If \p CheckMainHeader is true and \p IncludeName is a main header, returns<br>
-  // 0. Otherwise, returns the priority of the matching category or INT_MAX.<br>
-  // NOTE: this API is not thread-safe!<br>
-  int getIncludePriority(StringRef IncludeName, bool CheckMainHeader) const {<br>
-    int Ret = INT_MAX;<br>
-    for (unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i)<br>
-      if (CategoryRegexs[i].match(IncludeName)) {<br>
-        Ret = Style.IncludeStyle.IncludeCategories[i].Priority;<br>
-        break;<br>
-      }<br>
-    if (CheckMainHeader && IsMainFile && Ret > 0 && isMainHeader(IncludeName))<br>
-      Ret = 0;<br>
-    return Ret;<br>
-  }<br>
-<br>
-private:<br>
-  bool isMainHeader(StringRef IncludeName) const {<br>
-    if (!IncludeName.startswith("\""))<br>
-      return false;<br>
-    StringRef HeaderStem =<br>
-        llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(1));<br>
-    if (FileStem.startswith(HeaderStem) ||<br>
-        FileStem.startswith_lower(HeaderStem)) {<br>
-      llvm::Regex MainIncludeRegex(<br>
-          (HeaderStem + Style.IncludeStyle.IncludeIsMainRegex).str(),<br>
-          llvm::Regex::IgnoreCase);<br>
-      if (MainIncludeRegex.match(FileStem))<br>
-        return true;<br>
-    }<br>
-    return false;<br>
-  }<br>
-<br>
-  const FormatStyle &Style;<br>
-  bool IsMainFile;<br>
-  StringRef FileName;<br>
-  StringRef FileStem;<br>
-  // Regex is not thread-safe.<br>
-  mutable SmallVector<llvm::Regex, 4> CategoryRegexs;<br>
-};<br>
-<br>
 const char IncludeRegexPattern[] =<br>
     R"(^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">]))";<br>
<br>
@@ -1755,7 +1700,7 @@ tooling::Replacements sortCppIncludes(co<br>
   //<br>
   // FIXME: Do some sanity checking, e.g. edit distance of the base name, to fix<br>
   // cases where the first #include is unlikely to be the main header.<br>
-  IncludeCategoryManager Categories(Style, FileName);<br>
+  tooling::IncludeCategoryManager Categories(Style.IncludeStyle, FileName);<br>
   bool FirstIncludeBlock = true;<br>
   bool MainIncludeFound = false;<br>
   bool FormattingOff = false;<br>
@@ -1885,339 +1830,6 @@ inline bool isHeaderDeletion(const tooli<br>
   return Replace.getOffset() == UINT_MAX && Replace.getLength() == 1;<br>
 }<br>
<br>
-// Returns the offset after skipping a sequence of tokens, matched by \p<br>
-// GetOffsetAfterSequence, from the start of the code.<br>
-// \p GetOffsetAfterSequence should be a function that matches a sequence of<br>
-// tokens and returns an offset after the sequence.<br>
-unsigned getOffsetAfterTokenSequence(<br>
-    StringRef FileName, StringRef Code, const FormatStyle &Style,<br>
-    llvm::function_ref<unsigned(const SourceManager &, Lexer &, Token &)><br>
-        GetOffsetAfterSequence) {<br>
-  Environment Env(Code, FileName, /*Ranges=*/{});<br>
-  const SourceManager &SourceMgr = Env.getSourceManager();<br>
-  Lexer Lex(Env.getFileID(), SourceMgr.getBuffer(Env.getFileID()), SourceMgr,<br>
-            getFormattingLangOpts(Style));<br>
-  Token Tok;<br>
-  // Get the first token.<br>
-  Lex.LexFromRawLexer(Tok);<br>
-  return GetOffsetAfterSequence(SourceMgr, Lex, Tok);<br>
-}<br>
-<br>
-// Check if a sequence of tokens is like "#<Name> <raw_identifier>". If it is,<br>
-// \p Tok will be the token after this directive; otherwise, it can be any token<br>
-// after the given \p Tok (including \p Tok).<br>
-bool checkAndConsumeDirectiveWithName(Lexer &Lex, StringRef Name, Token &Tok) {<br>
-  bool Matched = Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) &&<br>
-                 Tok.is(tok::raw_identifier) &&<br>
-                 Tok.getRawIdentifier() == Name && !Lex.LexFromRawLexer(Tok) &&<br>
-                 Tok.is(tok::raw_identifier);<br>
-  if (Matched)<br>
-    Lex.LexFromRawLexer(Tok);<br>
-  return Matched;<br>
-}<br>
-<br>
-void skipComments(Lexer &Lex, Token &Tok) {<br>
-  while (Tok.is(tok::comment))<br>
-    if (Lex.LexFromRawLexer(Tok))<br>
-      return;<br>
-}<br>
-<br>
-// Returns the offset after header guard directives and any comments<br>
-// before/after header guards. If no header guard presents in the code, this<br>
-// will returns the offset after skipping all comments from the start of the<br>
-// code.<br>
-unsigned getOffsetAfterHeaderGuardsAndComments(StringRef FileName,<br>
-                                               StringRef Code,<br>
-                                               const FormatStyle &Style) {<br>
-  return getOffsetAfterTokenSequence(<br>
-      FileName, Code, Style,<br>
-      [](const SourceManager &SM, Lexer &Lex, Token Tok) {<br>
-        skipComments(Lex, Tok);<br>
-        unsigned InitialOffset = SM.getFileOffset(Tok.getLocation());<br>
-        if (checkAndConsumeDirectiveWithName(Lex, "ifndef", Tok)) {<br>
-          skipComments(Lex, Tok);<br>
-          if (checkAndConsumeDirectiveWithName(Lex, "define", Tok))<br>
-            return SM.getFileOffset(Tok.getLocation());<br>
-        }<br>
-        return InitialOffset;<br>
-      });<br>
-}<br>
-<br>
-// Check if a sequence of tokens is like<br>
-//    "#include ("header.h" | <header.h>)".<br>
-// If it is, \p Tok will be the token after this directive; otherwise, it can be<br>
-// any token after the given \p Tok (including \p Tok).<br>
-bool checkAndConsumeInclusiveDirective(Lexer &Lex, Token &Tok) {<br>
-  auto Matched = [&]() {<br>
-    Lex.LexFromRawLexer(Tok);<br>
-    return true;<br>
-  };<br>
-  if (Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) &&<br>
-      Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == "include") {<br>
-    if (Lex.LexFromRawLexer(Tok))<br>
-      return false;<br>
-    if (Tok.is(tok::string_literal))<br>
-      return Matched();<br>
-    if (Tok.is(tok::less)) {<br>
-      while (!Lex.LexFromRawLexer(Tok) && Tok.isNot(tok::greater)) {<br>
-      }<br>
-      if (Tok.is(tok::greater))<br>
-        return Matched();<br>
-    }<br>
-  }<br>
-  return false;<br>
-}<br>
-<br>
-// Returns the offset of the last #include directive after which a new<br>
-// #include can be inserted. This ignores #include's after the #include block(s)<br>
-// in the beginning of a file to avoid inserting headers into code sections<br>
-// where new #include's should not be added by default.<br>
-// These code sections include:<br>
-//      - raw string literals (containing #include).<br>
-//      - #if blocks.<br>
-//      - Special #include's among declarations (e.g. functions).<br>
-//<br>
-// If no #include after which a new #include can be inserted, this returns the<br>
-// offset after skipping all comments from the start of the code.<br>
-// Inserting after an #include is not allowed if it comes after code that is not<br>
-// #include (e.g. pre-processing directive that is not #include, declarations).<br>
-unsigned getMaxHeaderInsertionOffset(StringRef FileName, StringRef Code,<br>
-                                     const FormatStyle &Style) {<br>
-  return getOffsetAfterTokenSequence(<br>
-      FileName, Code, Style,<br>
-      [](const SourceManager &SM, Lexer &Lex, Token Tok) {<br>
-        skipComments(Lex, Tok);<br>
-        unsigned MaxOffset = SM.getFileOffset(Tok.getLocation());<br>
-        while (checkAndConsumeInclusiveDirective(Lex, Tok))<br>
-          MaxOffset = SM.getFileOffset(Tok.getLocation());<br>
-        return MaxOffset;<br>
-      });<br>
-}<br>
-<br>
-/// Generates replacements for inserting or deleting #include directives in a<br>
-/// file.<br>
-class HeaderIncludes {<br>
-public:<br>
-  HeaderIncludes(llvm::StringRef FileName, llvm::StringRef Code,<br>
-                 const FormatStyle &Style);<br>
-<br>
-  /// Inserts an #include directive of \p Header into the code. If \p IsAngled<br>
-  /// is true, \p Header will be quoted with <> in the directive; otherwise, it<br>
-  /// will be quoted with "".<br>
-  ///<br>
-  /// When searching for points to insert new header, this ignores #include's<br>
-  /// after the #include block(s) in the beginning of a file to avoid inserting<br>
-  /// headers into code sections where new #include's should not be added by<br>
-  /// default. These code sections include:<br>
-  ///   - raw string literals (containing #include).<br>
-  ///   - #if blocks.<br>
-  ///   - Special #include's among declarations (e.g. functions).<br>
-  ///<br>
-  /// Returns a replacement that inserts the new header into a suitable #include<br>
-  /// block of the same category. This respects the order of the existing<br>
-  /// #includes in the block; if the existing #includes are not already sorted,<br>
-  /// this will simply insert the #include in front of the first #include of the<br>
-  /// same category in the code that should be sorted after \p IncludeName. If<br>
-  /// \p IncludeName already exists (with exactly the same spelling), this<br>
-  /// returns None.<br>
-  llvm::Optional<tooling::Replacement> insert(llvm::StringRef Header,<br>
-                                              bool IsAngled) const;<br>
-<br>
-  /// Removes all existing #includes of \p Header quoted with <> if \p IsAngled<br>
-  /// is true or "" if \p IsAngled is false.<br>
-  /// This doesn't resolve the header file path; it only deletes #includes with<br>
-  /// exactly the same spelling.<br>
-  tooling::Replacements remove(llvm::StringRef Header, bool IsAngled) const;<br>
-<br>
-private:<br>
-  struct Include {<br>
-    Include(StringRef Name, tooling::Range R) : Name(Name), R(R) {}<br>
-<br>
-    // An include header quoted with either <> or "".<br>
-    std::string Name;<br>
-    // The range of the whole line of include directive including any eading<br>
-    // whitespaces and trailing comment.<br>
-    tooling::Range R;<br>
-  };<br>
-<br>
-  void addExistingInclude(Include IncludeToAdd, unsigned NextLineOffset);<br>
-<br>
-  std::string FileName;<br>
-  std::string Code;<br>
-<br>
-  // Map from include name (quotation trimmed) to a list of existing includes<br>
-  // (in case there are more than one) with the name in the current file. <x><br>
-  // and "x" will be treated as the same header when deleting #includes.<br>
-  llvm::StringMap<llvm::SmallVector<Include, 1>> ExistingIncludes;<br>
-<br>
-  /// Map from priorities of #include categories to all #includes in the same<br>
-  /// category. This is used to find #includes of the same category when<br>
-  /// inserting new #includes. #includes in the same categories are sorted in<br>
-  /// in the order they appear in the source file.<br>
-  /// See comment for "FormatStyle::IncludeCategories" for details about include<br>
-  /// priorities.<br>
-  std::unordered_map<int, llvm::SmallVector<const Include *, 8>><br>
-      IncludesByPriority;<br>
-<br>
-  int FirstIncludeOffset;<br>
-  // All new headers should be inserted after this offset (e.g. after header<br>
-  // guards, file comment).<br>
-  unsigned MinInsertOffset;<br>
-  // Max insertion offset in the original code. For example, we want to avoid<br>
-  // inserting new #includes into the actual code section (e.g. after a<br>
-  // declaration).<br>
-  unsigned MaxInsertOffset;<br>
-  IncludeCategoryManager Categories;<br>
-  // Record the offset of the end of the last include in each category.<br>
-  std::unordered_map<int, int> CategoryEndOffsets;<br>
-<br>
-  // All possible priorities.<br>
-  std::set<int> Priorities;<br>
-<br>
-  // Matches a whole #include directive.<br>
-  llvm::Regex IncludeRegex;<br>
-};<br>
-<br>
-HeaderIncludes::HeaderIncludes(StringRef FileName, StringRef Code,<br>
-                               const FormatStyle &Style)<br>
-    : FileName(FileName), Code(Code), FirstIncludeOffset(-1),<br>
-      MinInsertOffset(<br>
-          getOffsetAfterHeaderGuardsAndComments(FileName, Code, Style)),<br>
-      MaxInsertOffset(MinInsertOffset +<br>
-                      getMaxHeaderInsertionOffset(<br>
-                          FileName, Code.drop_front(MinInsertOffset), Style)),<br>
-      Categories(Style, FileName),<br>
-      IncludeRegex(llvm::Regex(IncludeRegexPattern)) {<br>
-  // Add 0 for main header and INT_MAX for headers that are not in any<br>
-  // category.<br>
-  Priorities = {0, INT_MAX};<br>
-  for (const auto &Category : Style.IncludeStyle.IncludeCategories)<br>
-    Priorities.insert(Category.Priority);<br>
-  SmallVector<StringRef, 32> Lines;<br>
-  Code.drop_front(MinInsertOffset).split(Lines, "\n");<br>
-<br>
-  unsigned Offset = MinInsertOffset;<br>
-  unsigned NextLineOffset;<br>
-  SmallVector<StringRef, 4> Matches;<br>
-  for (auto Line : Lines) {<br>
-    NextLineOffset = std::min(Code.size(), Offset + Line.size() + 1);<br>
-    if (IncludeRegex.match(Line, &Matches)) {<br>
-      // If this is the last line without trailing newline, we need to make<br>
-      // sure we don't delete across the file boundary.<br>
-      addExistingInclude(<br>
-          Include(Matches[2],<br>
-                  tooling::Range(<br>
-                      Offset, std::min(Line.size() + 1, Code.size() - Offset))),<br>
-          NextLineOffset);<br>
-    }<br>
-    Offset = NextLineOffset;<br>
-  }<br>
-<br>
-  // Populate CategoryEndOfssets:<br>
-  // - Ensure that CategoryEndOffset[Highest] is always populated.<br>
-  // - If CategoryEndOffset[Priority] isn't set, use the next higher value<br>
-  // that is set, up to CategoryEndOffset[Highest].<br>
-  auto Highest = Priorities.begin();<br>
-  if (CategoryEndOffsets.find(*Highest) == CategoryEndOffsets.end()) {<br>
-    if (FirstIncludeOffset >= 0)<br>
-      CategoryEndOffsets[*Highest] = FirstIncludeOffset;<br>
-    else<br>
-      CategoryEndOffsets[*Highest] = MinInsertOffset;<br>
-  }<br>
-  // By this point, CategoryEndOffset[Highest] is always set appropriately:<br>
-  //  - to an appropriate location before/after existing #includes, or<br>
-  //  - to right after the header guard, or<br>
-  //  - to the beginning of the file.<br>
-  for (auto I = ++Priorities.begin(), E = Priorities.end(); I != E; ++I)<br>
-    if (CategoryEndOffsets.find(*I) == CategoryEndOffsets.end())<br>
-      CategoryEndOffsets[*I] = CategoryEndOffsets[*std::prev(I)];<br>
-}<br>
-<br>
-inline StringRef trimInclude(StringRef IncludeName) {<br>
-  return IncludeName.trim("\"<>");<br>
-}<br>
-<br>
-// \p Offset: the start of the line following this include directive.<br>
-void HeaderIncludes::addExistingInclude(Include IncludeToAdd,<br>
-                                        unsigned NextLineOffset) {<br>
-  auto Iter =<br>
-      ExistingIncludes.try_emplace(trimInclude(IncludeToAdd.Name)).first;<br>
-  Iter->second.push_back(std::move(IncludeToAdd));<br>
-  auto &CurInclude = Iter->second.back();<br>
-  // The header name with quotes or angle brackets.<br>
-  // Only record the offset of current #include if we can insert after it.<br>
-  if (CurInclude.R.getOffset() <= MaxInsertOffset) {<br>
-    int Priority = Categories.getIncludePriority(<br>
-        CurInclude.Name, /*CheckMainHeader=*/FirstIncludeOffset < 0);<br>
-    CategoryEndOffsets[Priority] = NextLineOffset;<br>
-    IncludesByPriority[Priority].push_back(&CurInclude);<br>
-    if (FirstIncludeOffset < 0)<br>
-      FirstIncludeOffset = CurInclude.R.getOffset();<br>
-  }<br>
-}<br>
-<br>
-llvm::Optional<tooling::Replacement><br>
-HeaderIncludes::insert(llvm::StringRef IncludeName, bool IsAngled) const {<br>
-  assert(IncludeName == trimInclude(IncludeName));<br>
-  // If a <header> ("header") already exists in code, "header" (<header>) with<br>
-  // different quotation will still be inserted.<br>
-  // FIXME: figure out if this is the best behavior.<br>
-  auto It = ExistingIncludes.find(IncludeName);<br>
-  if (It != ExistingIncludes.end())<br>
-    for (const auto &Inc : It->second)<br>
-      if ((IsAngled && StringRef(Inc.Name).startswith("<")) ||<br>
-          (!IsAngled && StringRef(Inc.Name).startswith("\"")))<br>
-        return llvm::None;<br>
-  std::string Quoted = IsAngled ? ("<" + IncludeName + ">").str()<br>
-                                : ("\"" + IncludeName + "\"").str();<br>
-  StringRef QuotedName = Quoted;<br>
-  int Priority = Categories.getIncludePriority(<br>
-      QuotedName, /*CheckMainHeader=*/FirstIncludeOffset < 0);<br>
-  auto CatOffset = CategoryEndOffsets.find(Priority);<br>
-  assert(CatOffset != CategoryEndOffsets.end());<br>
-  unsigned InsertOffset = CatOffset->second; // Fall back offset<br>
-  auto Iter = IncludesByPriority.find(Priority);<br>
-  if (Iter != IncludesByPriority.end()) {<br>
-    for (const auto *Inc : Iter->second) {<br>
-      if (QuotedName < Inc->Name) {<br>
-        InsertOffset = Inc->R.getOffset();<br>
-        break;<br>
-      }<br>
-    }<br>
-  }<br>
-  assert(InsertOffset <= Code.size());<br>
-  std::string NewInclude = ("#include " + QuotedName + "\n").str();<br>
-  // When inserting headers at end of the code, also append '\n' to the code<br>
-  // if it does not end with '\n'.<br>
-  // FIXME: when inserting multiple #includes at the end of code, only one<br>
-  // newline should be added.<br>
-  if (InsertOffset == Code.size() && (!Code.empty() && Code.back() != '\n'))<br>
-    NewInclude = "\n" + NewInclude;<br>
-  return tooling::Replacement(FileName, InsertOffset, 0, NewInclude);<br>
-}<br>
-<br>
-tooling::Replacements HeaderIncludes::remove(llvm::StringRef IncludeName,<br>
-                                             bool IsAngled) const {<br>
-  assert(IncludeName == trimInclude(IncludeName));<br>
-  tooling::Replacements Result;<br>
-  auto Iter = ExistingIncludes.find(IncludeName);<br>
-  if (Iter == ExistingIncludes.end())<br>
-    return Result;<br>
-  for (const auto &Inc : Iter->second) {<br>
-    if ((IsAngled && StringRef(Inc.Name).startswith("\"")) ||<br>
-        (!IsAngled && StringRef(Inc.Name).startswith("<")))<br>
-      continue;<br>
-    llvm::Error Err = Result.add(tooling::Replacement(<br>
-        FileName, Inc.R.getOffset(), Inc.R.getLength(), ""));<br>
-    if (Err) {<br>
-      auto ErrMsg = "Unexpected conflicts in #include deletions: " +<br>
-                    llvm::toString(std::move(Err));<br>
-      llvm_unreachable(ErrMsg.c_str());<br>
-    }<br>
-  }<br>
-  return Result;<br>
-}<br>
-<br>
 // FIXME: insert empty lines between newly created blocks.<br>
 tooling::Replacements<br>
 fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces,<br>
@@ -2248,11 +1860,11 @@ fixCppIncludeInsertions(StringRef Code,<br>
<br>
<br>
   StringRef FileName = Replaces.begin()->getFilePath();<br>
-  HeaderIncludes Includes(FileName, Code, Style);<br>
+  tooling::HeaderIncludes Includes(FileName, Code, Style.IncludeStyle);<br>
<br>
   for (const auto &Header : HeadersToDelete) {<br>
     tooling::Replacements Replaces =<br>
-        Includes.remove(trimInclude(Header), Header.startswith("<"));<br>
+        Includes.remove(Header.trim("\"<>"), Header.startswith("<"));<br>
     for (const auto &R : Replaces) {<br>
       auto Err = Result.add(R);<br>
       if (Err) {<br>
@@ -2274,7 +1886,7 @@ fixCppIncludeInsertions(StringRef Code,<br>
     (void)Matched;<br>
     auto IncludeName = Matches[2];<br>
     auto Replace =<br>
-        Includes.insert(trimInclude(IncludeName), IncludeName.startswith("<"));<br>
+        Includes.insert(IncludeName.trim("\"<>"), IncludeName.startswith("<"));<br>
     if (Replace) {<br>
       auto Err = Result.add(*Replace);<br>
       if (Err) {<br>
<br>
Modified: cfe/trunk/lib/Tooling/Core/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Core/CMakeLists.txt?rev=332287&r1=332286&r2=332287&view=diff" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Core/CMakeLists.txt?rev=332287&r1=332286&r2=332287&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Tooling/Core/CMakeLists.txt (original)<br>
+++ cfe/trunk/lib/Tooling/Core/CMakeLists.txt Mon May 14 13:17:53 2018<br>
@@ -1,10 +1,11 @@<br>
 set(LLVM_LINK_COMPONENTS support)<br>
<br>
 add_clang_library(clangToolingCore<br>
+  Diagnostic.cpp<br>
+  HeaderIncludes.cpp<br>
   IncludeStyle.cpp<br>
   Lookup.cpp<br>
   Replacement.cpp<br>
-  Diagnostic.cpp<br>
<br>
   LINK_LIBS<br>
   clangAST<br>
<br>
Added: cfe/trunk/lib/Tooling/Core/HeaderIncludes.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Core/HeaderIncludes.cpp?rev=332287&view=auto" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Core/HeaderIncludes.cpp?rev=332287&view=auto</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Tooling/Core/HeaderIncludes.cpp (added)<br>
+++ cfe/trunk/lib/Tooling/Core/HeaderIncludes.cpp Mon May 14 13:17:53 2018<br>
@@ -0,0 +1,330 @@<br>
+//===--- HeaderIncludes.cpp - Insert/Delete #includes --*- C++ -*----------===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "clang/Tooling/Core/HeaderIncludes.h"<br>
+#include "clang/Basic/SourceManager.h"<br>
+#include "clang/Lex/Lexer.h"<br>
+<br>
+namespace clang {<br>
+namespace tooling {<br>
+namespace {<br>
+<br>
+LangOptions createLangOpts() {<br>
+  LangOptions LangOpts;<br>
+  LangOpts.CPlusPlus = 1;<br>
+  LangOpts.CPlusPlus11 = 1;<br>
+  LangOpts.CPlusPlus14 = 1;<br>
+  LangOpts.LineComment = 1;<br>
+  LangOpts.CXXOperatorNames = 1;<br>
+  LangOpts.Bool = 1;<br>
+  LangOpts.ObjC1 = 1;<br>
+  LangOpts.ObjC2 = 1;<br>
+  LangOpts.MicrosoftExt = 1;    // To get kw___try, kw___finally.<br>
+  LangOpts.DeclSpecKeyword = 1; // To get __declspec.<br>
+  LangOpts.WChar = 1;           // To get wchar_t<br>
+  return LangOpts;<br>
+}<br>
+<br>
+// Returns the offset after skipping a sequence of tokens, matched by \p<br>
+// GetOffsetAfterSequence, from the start of the code.<br>
+// \p GetOffsetAfterSequence should be a function that matches a sequence of<br>
+// tokens and returns an offset after the sequence.<br>
+unsigned getOffsetAfterTokenSequence(<br>
+    StringRef FileName, StringRef Code, const IncludeStyle &Style,<br>
+    llvm::function_ref<unsigned(const SourceManager &, Lexer &, Token &)><br>
+        GetOffsetAfterSequence) {<br>
+  SourceManagerForFile VirtualSM(FileName, Code);<br>
+  SourceManager &SM = VirtualSM.get();<br>
+  Lexer Lex(SM.getMainFileID(), SM.getBuffer(SM.getMainFileID()), SM,<br>
+            createLangOpts());<br>
+  Token Tok;<br>
+  // Get the first token.<br>
+  Lex.LexFromRawLexer(Tok);<br>
+  return GetOffsetAfterSequence(SM, Lex, Tok);<br>
+}<br>
+<br>
+// Check if a sequence of tokens is like "#<Name> <raw_identifier>". If it is,<br>
+// \p Tok will be the token after this directive; otherwise, it can be any token<br>
+// after the given \p Tok (including \p Tok).<br>
+bool checkAndConsumeDirectiveWithName(Lexer &Lex, StringRef Name, Token &Tok) {<br>
+  bool Matched = Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) &&<br>
+                 Tok.is(tok::raw_identifier) &&<br>
+                 Tok.getRawIdentifier() == Name && !Lex.LexFromRawLexer(Tok) &&<br>
+                 Tok.is(tok::raw_identifier);<br>
+  if (Matched)<br>
+    Lex.LexFromRawLexer(Tok);<br>
+  return Matched;<br>
+}<br>
+<br>
+void skipComments(Lexer &Lex, Token &Tok) {<br>
+  while (Tok.is(tok::comment))<br>
+    if (Lex.LexFromRawLexer(Tok))<br>
+      return;<br>
+}<br>
+<br>
+// Returns the offset after header guard directives and any comments<br>
+// before/after header guards. If no header guard presents in the code, this<br>
+// will returns the offset after skipping all comments from the start of the<br>
+// code.<br>
+unsigned getOffsetAfterHeaderGuardsAndComments(StringRef FileName,<br>
+                                               StringRef Code,<br>
+                                               const IncludeStyle &Style) {<br>
+  return getOffsetAfterTokenSequence(<br>
+      FileName, Code, Style,<br>
+      [](const SourceManager &SM, Lexer &Lex, Token Tok) {<br>
+        skipComments(Lex, Tok);<br>
+        unsigned InitialOffset = SM.getFileOffset(Tok.getLocation());<br>
+        if (checkAndConsumeDirectiveWithName(Lex, "ifndef", Tok)) {<br>
+          skipComments(Lex, Tok);<br>
+          if (checkAndConsumeDirectiveWithName(Lex, "define", Tok))<br>
+            return SM.getFileOffset(Tok.getLocation());<br>
+        }<br>
+        return InitialOffset;<br>
+      });<br>
+}<br>
+<br>
+// Check if a sequence of tokens is like<br>
+//    "#include ("header.h" | <header.h>)".<br>
+// If it is, \p Tok will be the token after this directive; otherwise, it can be<br>
+// any token after the given \p Tok (including \p Tok).<br>
+bool checkAndConsumeInclusiveDirective(Lexer &Lex, Token &Tok) {<br>
+  auto Matched = [&]() {<br>
+    Lex.LexFromRawLexer(Tok);<br>
+    return true;<br>
+  };<br>
+  if (Tok.is(tok::hash) && !Lex.LexFromRawLexer(Tok) &&<br>
+      Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == "include") {<br>
+    if (Lex.LexFromRawLexer(Tok))<br>
+      return false;<br>
+    if (Tok.is(tok::string_literal))<br>
+      return Matched();<br>
+    if (Tok.is(tok::less)) {<br>
+      while (!Lex.LexFromRawLexer(Tok) && Tok.isNot(tok::greater)) {<br>
+      }<br>
+      if (Tok.is(tok::greater))<br>
+        return Matched();<br>
+    }<br>
+  }<br>
+  return false;<br>
+}<br>
+<br>
+// Returns the offset of the last #include directive after which a new<br>
+// #include can be inserted. This ignores #include's after the #include block(s)<br>
+// in the beginning of a file to avoid inserting headers into code sections<br>
+// where new #include's should not be added by default.<br>
+// These code sections include:<br>
+//      - raw string literals (containing #include).<br>
+//      - #if blocks.<br>
+//      - Special #include's among declarations (e.g. functions).<br>
+//<br>
+// If no #include after which a new #include can be inserted, this returns the<br>
+// offset after skipping all comments from the start of the code.<br>
+// Inserting after an #include is not allowed if it comes after code that is not<br>
+// #include (e.g. pre-processing directive that is not #include, declarations).<br>
+unsigned getMaxHeaderInsertionOffset(StringRef FileName, StringRef Code,<br>
+                                     const IncludeStyle &Style) {<br>
+  return getOffsetAfterTokenSequence(<br>
+      FileName, Code, Style,<br>
+      [](const SourceManager &SM, Lexer &Lex, Token Tok) {<br>
+        skipComments(Lex, Tok);<br>
+        unsigned MaxOffset = SM.getFileOffset(Tok.getLocation());<br>
+        while (checkAndConsumeInclusiveDirective(Lex, Tok))<br>
+          MaxOffset = SM.getFileOffset(Tok.getLocation());<br>
+        return MaxOffset;<br>
+      });<br>
+}<br>
+<br>
+inline StringRef trimInclude(StringRef IncludeName) {<br>
+  return IncludeName.trim("\"<>");<br>
+}<br>
+<br>
+const char IncludeRegexPattern[] =<br>
+    R"(^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">]))";<br>
+<br>
+} // anonymous namespace<br>
+<br>
+IncludeCategoryManager::IncludeCategoryManager(const IncludeStyle &Style,<br>
+                                               StringRef FileName)<br>
+    : Style(Style), FileName(FileName) {<br>
+  FileStem = llvm::sys::path::stem(FileName);<br>
+  for (const auto &Category : Style.IncludeCategories)<br>
+    CategoryRegexs.emplace_back(Category.Regex, llvm::Regex::IgnoreCase);<br>
+  IsMainFile = FileName.endswith(".c") || FileName.endswith(".cc") ||<br>
+               FileName.endswith(".cpp") || FileName.endswith(".c++") ||<br>
+               FileName.endswith(".cxx") || FileName.endswith(".m") ||<br>
+               FileName.endswith(".mm");<br>
+}<br>
+<br>
+int IncludeCategoryManager::getIncludePriority(StringRef IncludeName,<br>
+                                               bool CheckMainHeader) const {<br>
+  int Ret = INT_MAX;<br>
+  for (unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i)<br>
+    if (CategoryRegexs[i].match(IncludeName)) {<br>
+      Ret = Style.IncludeCategories[i].Priority;<br>
+      break;<br>
+    }<br>
+  if (CheckMainHeader && IsMainFile && Ret > 0 && isMainHeader(IncludeName))<br>
+    Ret = 0;<br>
+  return Ret;<br>
+}<br>
+<br>
+bool IncludeCategoryManager::isMainHeader(StringRef IncludeName) const {<br>
+  if (!IncludeName.startswith("\""))<br>
+    return false;<br>
+  StringRef HeaderStem =<br>
+      llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(1));<br>
+  if (FileStem.startswith(HeaderStem) ||<br>
+      FileStem.startswith_lower(HeaderStem)) {<br>
+    llvm::Regex MainIncludeRegex((HeaderStem + Style.IncludeIsMainRegex).str(),<br>
+                                 llvm::Regex::IgnoreCase);<br>
+    if (MainIncludeRegex.match(FileStem))<br>
+      return true;<br>
+  }<br>
+  return false;<br>
+}<br>
+<br>
+HeaderIncludes::HeaderIncludes(StringRef FileName, StringRef Code,<br>
+                               const IncludeStyle &Style)<br>
+    : FileName(FileName), Code(Code), FirstIncludeOffset(-1),<br>
+      MinInsertOffset(<br>
+          getOffsetAfterHeaderGuardsAndComments(FileName, Code, Style)),<br>
+      MaxInsertOffset(MinInsertOffset +<br>
+                      getMaxHeaderInsertionOffset(<br>
+                          FileName, Code.drop_front(MinInsertOffset), Style)),<br>
+      Categories(Style, FileName),<br>
+      IncludeRegex(llvm::Regex(IncludeRegexPattern)) {<br>
+  // Add 0 for main header and INT_MAX for headers that are not in any<br>
+  // category.<br>
+  Priorities = {0, INT_MAX};<br>
+  for (const auto &Category : Style.IncludeCategories)<br>
+    Priorities.insert(Category.Priority);<br>
+  SmallVector<StringRef, 32> Lines;<br>
+  Code.drop_front(MinInsertOffset).split(Lines, "\n");<br>
+<br>
+  unsigned Offset = MinInsertOffset;<br>
+  unsigned NextLineOffset;<br>
+  SmallVector<StringRef, 4> Matches;<br>
+  for (auto Line : Lines) {<br>
+    NextLineOffset = std::min(Code.size(), Offset + Line.size() + 1);<br>
+    if (IncludeRegex.match(Line, &Matches)) {<br>
+      // If this is the last line without trailing newline, we need to make<br>
+      // sure we don't delete across the file boundary.<br>
+      addExistingInclude(<br>
+          Include(Matches[2],<br>
+                  tooling::Range(<br>
+                      Offset, std::min(Line.size() + 1, Code.size() - Offset))),<br>
+          NextLineOffset);<br>
+    }<br>
+    Offset = NextLineOffset;<br>
+  }<br>
+<br>
+  // Populate CategoryEndOfssets:<br>
+  // - Ensure that CategoryEndOffset[Highest] is always populated.<br>
+  // - If CategoryEndOffset[Priority] isn't set, use the next higher value<br>
+  // that is set, up to CategoryEndOffset[Highest].<br>
+  auto Highest = Priorities.begin();<br>
+  if (CategoryEndOffsets.find(*Highest) == CategoryEndOffsets.end()) {<br>
+    if (FirstIncludeOffset >= 0)<br>
+      CategoryEndOffsets[*Highest] = FirstIncludeOffset;<br>
+    else<br>
+      CategoryEndOffsets[*Highest] = MinInsertOffset;<br>
+  }<br>
+  // By this point, CategoryEndOffset[Highest] is always set appropriately:<br>
+  //  - to an appropriate location before/after existing #includes, or<br>
+  //  - to right after the header guard, or<br>
+  //  - to the beginning of the file.<br>
+  for (auto I = ++Priorities.begin(), E = Priorities.end(); I != E; ++I)<br>
+    if (CategoryEndOffsets.find(*I) == CategoryEndOffsets.end())<br>
+      CategoryEndOffsets[*I] = CategoryEndOffsets[*std::prev(I)];<br>
+}<br>
+<br>
+// \p Offset: the start of the line following this include directive.<br>
+void HeaderIncludes::addExistingInclude(Include IncludeToAdd,<br>
+                                        unsigned NextLineOffset) {<br>
+  auto Iter =<br>
+      ExistingIncludes.try_emplace(trimInclude(IncludeToAdd.Name)).first;<br>
+  Iter->second.push_back(std::move(IncludeToAdd));<br>
+  auto &CurInclude = Iter->second.back();<br>
+  // The header name with quotes or angle brackets.<br>
+  // Only record the offset of current #include if we can insert after it.<br>
+  if (CurInclude.R.getOffset() <= MaxInsertOffset) {<br>
+    int Priority = Categories.getIncludePriority(<br>
+        CurInclude.Name, /*CheckMainHeader=*/FirstIncludeOffset < 0);<br>
+    CategoryEndOffsets[Priority] = NextLineOffset;<br>
+    IncludesByPriority[Priority].push_back(&CurInclude);<br>
+    if (FirstIncludeOffset < 0)<br>
+      FirstIncludeOffset = CurInclude.R.getOffset();<br>
+  }<br>
+}<br>
+<br>
+llvm::Optional<tooling::Replacement><br>
+HeaderIncludes::insert(llvm::StringRef IncludeName, bool IsAngled) const {<br>
+  assert(IncludeName == trimInclude(IncludeName));<br>
+  // If a <header> ("header") already exists in code, "header" (<header>) with<br>
+  // different quotation will still be inserted.<br>
+  // FIXME: figure out if this is the best behavior.<br>
+  auto It = ExistingIncludes.find(IncludeName);<br>
+  if (It != ExistingIncludes.end())<br>
+    for (const auto &Inc : It->second)<br>
+      if ((IsAngled && StringRef(Inc.Name).startswith("<")) ||<br>
+          (!IsAngled && StringRef(Inc.Name).startswith("\"")))<br>
+        return llvm::None;<br>
+  std::string Quoted = IsAngled ? ("<" + IncludeName + ">").str()<br>
+                                : ("\"" + IncludeName + "\"").str();<br>
+  StringRef QuotedName = Quoted;<br>
+  int Priority = Categories.getIncludePriority(<br>
+      QuotedName, /*CheckMainHeader=*/FirstIncludeOffset < 0);<br>
+  auto CatOffset = CategoryEndOffsets.find(Priority);<br>
+  assert(CatOffset != CategoryEndOffsets.end());<br>
+  unsigned InsertOffset = CatOffset->second; // Fall back offset<br>
+  auto Iter = IncludesByPriority.find(Priority);<br>
+  if (Iter != IncludesByPriority.end()) {<br>
+    for (const auto *Inc : Iter->second) {<br>
+      if (QuotedName < Inc->Name) {<br>
+        InsertOffset = Inc->R.getOffset();<br>
+        break;<br>
+      }<br>
+    }<br>
+  }<br>
+  assert(InsertOffset <= Code.size());<br>
+  std::string NewInclude = ("#include " + QuotedName + "\n").str();<br>
+  // When inserting headers at end of the code, also append '\n' to the code<br>
+  // if it does not end with '\n'.<br>
+  // FIXME: when inserting multiple #includes at the end of code, only one<br>
+  // newline should be added.<br>
+  if (InsertOffset == Code.size() && (!Code.empty() && Code.back() != '\n'))<br>
+    NewInclude = "\n" + NewInclude;<br>
+  return tooling::Replacement(FileName, InsertOffset, 0, NewInclude);<br>
+}<br>
+<br>
+tooling::Replacements HeaderIncludes::remove(llvm::StringRef IncludeName,<br>
+                                             bool IsAngled) const {<br>
+  assert(IncludeName == trimInclude(IncludeName));<br>
+  tooling::Replacements Result;<br>
+  auto Iter = ExistingIncludes.find(IncludeName);<br>
+  if (Iter == ExistingIncludes.end())<br>
+    return Result;<br>
+  for (const auto &Inc : Iter->second) {<br>
+    if ((IsAngled && StringRef(Inc.Name).startswith("\"")) ||<br>
+        (!IsAngled && StringRef(Inc.Name).startswith("<")))<br>
+      continue;<br>
+    llvm::Error Err = Result.add(tooling::Replacement(<br>
+        FileName, Inc.R.getOffset(), Inc.R.getLength(), ""));<br>
+    if (Err) {<br>
+      auto ErrMsg = "Unexpected conflicts in #include deletions: " +<br>
+                    llvm::toString(std::move(Err));<br>
+      llvm_unreachable(ErrMsg.c_str());<br>
+    }<br>
+  }<br>
+  return Result;<br>
+}<br>
+<br>
+<br>
+} // namespace tooling<br>
+} // namespace clang<br>
<br>
Modified: cfe/trunk/unittests/Format/CleanupTest.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/CleanupTest.cpp?rev=332287&r1=332286&r2=332287&view=diff" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/CleanupTest.cpp?rev=332287&r1=332286&r2=332287&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/unittests/Format/CleanupTest.cpp (original)<br>
+++ cfe/trunk/unittests/Format/CleanupTest.cpp Mon May 14 13:17:53 2018<br>
@@ -358,175 +358,6 @@ TEST_F(CleanUpReplacementsTest, FixOnlyA<br>
   EXPECT_EQ(Expected, formatAndApply(Code, Replaces));<br>
 }<br>
<br>
-TEST_F(CleanUpReplacementsTest, NoExistingIncludeWithoutDefine) {<br>
-  std::string Code = "int main() {}";<br>
-  std::string Expected = "#include \"a.h\"\n"<br>
-                         "int main() {}";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include \"a.h\"")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, NoExistingIncludeWithDefine) {<br>
-  std::string Code = "#ifndef A_H\n"<br>
-                     "#define A_H\n"<br>
-                     "class A {};\n"<br>
-                     "#define MMM 123\n"<br>
-                     "#endif";<br>
-  std::string Expected = "#ifndef A_H\n"<br>
-                         "#define A_H\n"<br>
-                         "#include \"b.h\"\n"<br>
-                         "class A {};\n"<br>
-                         "#define MMM 123\n"<br>
-                         "#endif";<br>
-<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include \"b.h\"")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, InsertBeforeCategoryWithLowerPriority) {<br>
-  std::string Code = "#ifndef A_H\n"<br>
-                     "#define A_H\n"<br>
-                     "\n"<br>
-                     "\n"<br>
-                     "\n"<br>
-                     "#include <vector>\n"<br>
-                     "class A {};\n"<br>
-                     "#define MMM 123\n"<br>
-                     "#endif";<br>
-  std::string Expected = "#ifndef A_H\n"<br>
-                         "#define A_H\n"<br>
-                         "\n"<br>
-                         "\n"<br>
-                         "\n"<br>
-                         "#include \"a.h\"\n"<br>
-                         "#include <vector>\n"<br>
-                         "class A {};\n"<br>
-                         "#define MMM 123\n"<br>
-                         "#endif";<br>
-<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include \"a.h\"")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, InsertAfterMainHeader) {<br>
-  std::string Code = "#include \"fix.h\"\n"<br>
-                     "\n"<br>
-                     "int main() {}";<br>
-  std::string Expected = "#include \"fix.h\"\n"<br>
-                         "#include <a>\n"<br>
-                         "\n"<br>
-                         "int main() {}";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include <a>")});<br>
-  Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, InsertBeforeSystemHeaderLLVM) {<br>
-  std::string Code = "#include <memory>\n"<br>
-                     "\n"<br>
-                     "int main() {}";<br>
-  std::string Expected = "#include \"z.h\"\n"<br>
-                         "#include <memory>\n"<br>
-                         "\n"<br>
-                         "int main() {}";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include \"z.h\"")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, InsertAfterSystemHeaderGoogle) {<br>
-  std::string Code = "#include <memory>\n"<br>
-                     "\n"<br>
-                     "int main() {}";<br>
-  std::string Expected = "#include <memory>\n"<br>
-                         "#include \"z.h\"\n"<br>
-                         "\n"<br>
-                         "int main() {}";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include \"z.h\"")});<br>
-  Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, InsertOneIncludeLLVMStyle) {<br>
-  std::string Code = "#include \"x/fix.h\"\n"<br>
-                     "#include \"a.h\"\n"<br>
-                     "#include \"b.h\"\n"<br>
-                     "#include \"clang/Format/Format.h\"\n"<br>
-                     "#include <memory>\n";<br>
-  std::string Expected = "#include \"x/fix.h\"\n"<br>
-                         "#include \"a.h\"\n"<br>
-                         "#include \"b.h\"\n"<br>
-                         "#include \"d.h\"\n"<br>
-                         "#include \"clang/Format/Format.h\"\n"<br>
-                         "#include \"llvm/x/y.h\"\n"<br>
-                         "#include <memory>\n";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include \"d.h\""),<br>
-                      createInsertion("#include \"llvm/x/y.h\"")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, InsertIntoBlockSorted) {<br>
-  std::string Code = "#include \"x/fix.h\"\n"<br>
-                     "#include \"a.h\"\n"<br>
-                     "#include \"c.h\"\n"<br>
-                     "#include <memory>\n";<br>
-  std::string Expected = "#include \"x/fix.h\"\n"<br>
-                         "#include \"a.h\"\n"<br>
-                         "#include \"b.h\"\n"<br>
-                         "#include \"c.h\"\n"<br>
-                         "#include <memory>\n";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include \"b.h\"")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, InsertIntoFirstBlockOfSameKind) {<br>
-  std::string Code = "#include \"x/fix.h\"\n"<br>
-                     "#include \"c.h\"\n"<br>
-                     "#include \"e.h\"\n"<br>
-                     "#include \"f.h\"\n"<br>
-                     "#include <memory>\n"<br>
-                     "#include <vector>\n"<br>
-                     "#include \"m.h\"\n"<br>
-                     "#include \"n.h\"\n";<br>
-  std::string Expected = "#include \"x/fix.h\"\n"<br>
-                         "#include \"c.h\"\n"<br>
-                         "#include \"d.h\"\n"<br>
-                         "#include \"e.h\"\n"<br>
-                         "#include \"f.h\"\n"<br>
-                         "#include <memory>\n"<br>
-                         "#include <vector>\n"<br>
-                         "#include \"m.h\"\n"<br>
-                         "#include \"n.h\"\n";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include \"d.h\"")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, InsertIntoSystemBlockSorted) {<br>
-  std::string Code = "#include \"x/fix.h\"\n"<br>
-                     "#include \"a.h\"\n"<br>
-                     "#include \"c.h\"\n"<br>
-                     "#include <a>\n"<br>
-                     "#include <z>\n";<br>
-  std::string Expected = "#include \"x/fix.h\"\n"<br>
-                         "#include \"a.h\"\n"<br>
-                         "#include \"c.h\"\n"<br>
-                         "#include <a>\n"<br>
-                         "#include <vector>\n"<br>
-                         "#include <z>\n";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include <vector>")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-<br>
 TEST_F(CleanUpReplacementsTest, InsertMultipleIncludesLLVMStyle) {<br>
   std::string Code = "#include \"x/fix.h\"\n"<br>
                      "#include \"a.h\"\n"<br>
@@ -548,24 +379,6 @@ TEST_F(CleanUpReplacementsTest, InsertMu<br>
   EXPECT_EQ(Expected, apply(Code, Replaces));<br>
 }<br>
<br>
-TEST_F(CleanUpReplacementsTest, InsertNewSystemIncludeGoogleStyle) {<br>
-  std::string Code = "#include \"x/fix.h\"\n"<br>
-                     "\n"<br>
-                     "#include \"y/a.h\"\n"<br>
-                     "#include \"z/b.h\"\n";<br>
-  // FIXME: inserting after the empty line following the main header might be<br>
-  // preferred.<br>
-  std::string Expected = "#include \"x/fix.h\"\n"<br>
-                         "#include <vector>\n"<br>
-                         "\n"<br>
-                         "#include \"y/a.h\"\n"<br>
-                         "#include \"z/b.h\"\n";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include <vector>")});<br>
-  Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
 TEST_F(CleanUpReplacementsTest, InsertMultipleIncludesGoogleStyle) {<br>
   std::string Code = "#include \"x/fix.h\"\n"<br>
                      "\n"<br>
@@ -623,6 +436,19 @@ TEST_F(CleanUpReplacementsTest, InsertMu<br>
   EXPECT_EQ(Expected, formatAndApply(Code, Replaces));<br>
 }<br>
<br>
+TEST_F(CleanUpReplacementsTest, NoNewLineAtTheEndOfCodeMultipleInsertions) {<br>
+  std::string Code = "#include <map>";<br>
+  // FIXME: a better behavior is to only append on newline to Code, but this<br>
+  // case should be rare in practice.<br>
+  std::string Expected =<br>
+      "#include <map>\n#include <string>\n\n#include <vector>\n";<br>
+  tooling::Replacements Replaces =<br>
+      toReplacements({createInsertion("#include <string>"),<br>
+                      createInsertion("#include <vector>")});<br>
+  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
+}<br>
+<br>
+<br>
 TEST_F(CleanUpReplacementsTest, FormatCorrectLineWhenHeadersAreInserted) {<br>
   std::string Code = "\n"<br>
                      "int x;\n"<br>
@@ -648,227 +474,6 @@ TEST_F(CleanUpReplacementsTest, FormatCo<br>
   EXPECT_EQ(Expected, formatAndApply(Code, Replaces));<br>
 }<br>
<br>
-TEST_F(CleanUpReplacementsTest, NotConfusedByDefine) {<br>
-  std::string Code = "void f() {}\n"<br>
-                     "#define A \\\n"<br>
-                     "  int i;";<br>
-  std::string Expected = "#include <vector>\n"<br>
-                         "void f() {}\n"<br>
-                         "#define A \\\n"<br>
-                         "  int i;";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include <vector>")});<br>
-  EXPECT_EQ(Expected, formatAndApply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, SkippedTopComment) {<br>
-  std::string Code = "// comment\n"<br>
-                     "\n"<br>
-                     "   // comment\n";<br>
-  std::string Expected = "// comment\n"<br>
-                         "\n"<br>
-                         "   // comment\n"<br>
-                         "#include <vector>\n";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include <vector>")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, SkippedMixedComments) {<br>
-  std::string Code = "// comment\n"<br>
-                     "// comment \\\n"<br>
-                     " comment continued\n"<br>
-                     "/*\n"<br>
-                     "* comment\n"<br>
-                     "*/\n";<br>
-  std::string Expected = "// comment\n"<br>
-                         "// comment \\\n"<br>
-                         " comment continued\n"<br>
-                         "/*\n"<br>
-                         "* comment\n"<br>
-                         "*/\n"<br>
-                         "#include <vector>\n";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include <vector>")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, MultipleBlockCommentsInOneLine) {<br>
-  std::string Code = "/*\n"<br>
-                     "* comment\n"<br>
-                     "*/ /* comment\n"<br>
-                     "*/\n"<br>
-                     "\n\n"<br>
-                     "/* c1 */ /*c2 */\n";<br>
-  std::string Expected = "/*\n"<br>
-                         "* comment\n"<br>
-                         "*/ /* comment\n"<br>
-                         "*/\n"<br>
-                         "\n\n"<br>
-                         "/* c1 */ /*c2 */\n"<br>
-                         "#include <vector>\n";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include <vector>")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, CodeAfterComments) {<br>
-  std::string Code = "/*\n"<br>
-                     "* comment\n"<br>
-                     "*/ /* comment\n"<br>
-                     "*/\n"<br>
-                     "\n\n"<br>
-                     "/* c1 */ /*c2 */\n"<br>
-                     "\n"<br>
-                     "int x;\n";<br>
-  std::string Expected = "/*\n"<br>
-                         "* comment\n"<br>
-                         "*/ /* comment\n"<br>
-                         "*/\n"<br>
-                         "\n\n"<br>
-                         "/* c1 */ /*c2 */\n"<br>
-                         "\n"<br>
-                         "#include <vector>\n"<br>
-                         "int x;\n";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include <vector>")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, FakeHeaderGuardIfDef) {<br>
-  std::string Code = "// comment \n"<br>
-                     "#ifdef X\n"<br>
-                     "#define X\n";<br>
-  std::string Expected = "// comment \n"<br>
-                         "#include <vector>\n"<br>
-                         "#ifdef X\n"<br>
-                         "#define X\n";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include <vector>")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, RealHeaderGuardAfterComments) {<br>
-  std::string Code = "// comment \n"<br>
-                     "#ifndef X\n"<br>
-                     "#define X\n"<br>
-                     "int x;\n"<br>
-                     "#define Y 1\n";<br>
-  std::string Expected = "// comment \n"<br>
-                         "#ifndef X\n"<br>
-                         "#define X\n"<br>
-                         "#include <vector>\n"<br>
-                         "int x;\n"<br>
-                         "#define Y 1\n";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include <vector>")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, IfNDefWithNoDefine) {<br>
-  std::string Code = "// comment \n"<br>
-                     "#ifndef X\n"<br>
-                     "int x;\n"<br>
-                     "#define Y 1\n";<br>
-  std::string Expected = "// comment \n"<br>
-                         "#include <vector>\n"<br>
-                         "#ifndef X\n"<br>
-                         "int x;\n"<br>
-                         "#define Y 1\n";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include <vector>")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, FakeHeaderGuard) {<br>
-  std::string Code = "// comment \n"<br>
-                     "#ifndef X\n"<br>
-                     "#define 1\n";<br>
-  std::string Expected = "// comment \n"<br>
-                         "#include <vector>\n"<br>
-                         "#ifndef X\n"<br>
-                         "#define 1\n";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include <vector>")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, HeaderGuardWithComment) {<br>
-  std::string Code = "// comment \n"<br>
-                     "#ifndef X // comment\n"<br>
-                     "// comment\n"<br>
-                     "/* comment\n"<br>
-                     "*/\n"<br>
-                     "/* comment */ #define X\n"<br>
-                     "int x;\n"<br>
-                     "#define Y 1\n";<br>
-  std::string Expected = "// comment \n"<br>
-                         "#ifndef X // comment\n"<br>
-                         "// comment\n"<br>
-                         "/* comment\n"<br>
-                         "*/\n"<br>
-                         "/* comment */ #define X\n"<br>
-                         "#include <vector>\n"<br>
-                         "int x;\n"<br>
-                         "#define Y 1\n";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include <vector>")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, EmptyCode) {<br>
-  std::string Code = "";<br>
-  std::string Expected = "#include <vector>\n";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include <vector>")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, NoNewLineAtTheEndOfCode) {<br>
-  std::string Code = "#include <map>";<br>
-  std::string Expected = "#include <map>\n#include <vector>\n";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include <vector>")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, NoNewLineAtTheEndOfCodeMultipleInsertions) {<br>
-  std::string Code = "#include <map>";<br>
-  // FIXME: a better behavior is to only append on newline to Code, but this<br>
-  // case should be rare in practice.<br>
-  std::string Expected =<br>
-      "#include <map>\n#include <string>\n\n#include <vector>\n";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include <string>"),<br>
-                      createInsertion("#include <vector>")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, SkipExistingHeaders) {<br>
-  std::string Code = "#include \"a.h\"\n"<br>
-                     "#include <vector>\n";<br>
-  std::string Expected = "#include \"a.h\"\n"<br>
-                         "#include <vector>\n";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include <vector>"),<br>
-                      createInsertion("#include \"a.h\"")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, AddIncludesWithDifferentForms) {<br>
-  std::string Code = "#include \"a.h\"\n"<br>
-                     "#include <vector>\n";<br>
-  // FIXME: this might not be the best behavior.<br>
-  std::string Expected = "#include \"a.h\"\n"<br>
-                         "#include \"vector\"\n"<br>
-                         "#include <a.h>\n"<br>
-                         "#include <vector>\n";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createInsertion("#include \"vector\""),<br>
-                      createInsertion("#include <a.h>")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
 TEST_F(CleanUpReplacementsTest, SimpleDeleteIncludes) {<br>
   std::string Code = "#include \"abc.h\"\n"<br>
                      "#include \"xyz.h\" // comment\n"<br>
@@ -881,35 +486,6 @@ TEST_F(CleanUpReplacementsTest, SimpleDe<br>
   EXPECT_EQ(Expected, apply(Code, Replaces));<br>
 }<br>
<br>
-TEST_F(CleanUpReplacementsTest, DeleteAllCode) {<br>
-  std::string Code = "#include \"xyz.h\"\n"<br>
-                     "#include <xyz.h>";<br>
-  std::string Expected = "";<br>
-  tooling::Replacements Replaces =<br>
-      toReplacements({createDeletion("\"xyz.h\""), createDeletion("<xyz.h>")});<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br>
-TEST_F(CleanUpReplacementsTest, DeleteOnlyIncludesWithSameQuote) {<br>
-  std::string Code = "#include \"xyz.h\"\n"<br>
-                     "#include \"xyz\"\n"<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-                     "#include <xyz.h>\n";<br>
-  std::string Expected = "#include \"xyz.h\"\n"<br>
-                         "#include \"xyz\"\n";<br>
-  tooling::Replacements Replaces = toReplacements({createDeletion("<xyz.h>")});</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-TEST_F(CleanUpReplacementsTest, OnlyDeleteHeaderWithType) {</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  std::string Code = "#include \"xyz.h\"\n"<br>
-                     "#include \"xyz\"\n"<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-                     "#include <xyz.h>";<br>
-  std::string Expected = "#include \"xyz.h\"\n"<br>
-                         "#include \"xyz\"\n";<br>
-  tooling::Replacements Replaces = toReplacements({createDeletion("<xyz.h>")});</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
 TEST_F(CleanUpReplacementsTest, InsertionAndDeleteHeader) {</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
   std::string Code = "#include \"a.h\"\n"<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
                      "\n"<br>
@@ -922,117 +498,6 @@ TEST_F(CleanUpReplacementsTest, Insertio<br>
   EXPECT_EQ(Expected, apply(Code, Replaces));<br>
 }<br>
<br>
-TEST_F(CleanUpReplacementsTest, NoInsertionAfterCode) {</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  std::string Code = "#include \"a.h\"\n"<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-                     "void f() {}\n"<br>
-                     "#include \"b.h\"\n";</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  std::string Expected = "#include \"a.h\"\n"<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-                         "#include \"c.h\"\n"<br>
-                         "void f() {}\n"<br>
-                         "#include \"b.h\"\n";<br>
-  tooling::Replacements Replaces = toReplacements(<br>
-      {createInsertion("#include \"c.h\"")});</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-TEST_F(CleanUpReplacementsTest, NoInsertionInStringLiteral) {</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  std::string Code = "#include \"a.h\"\n"<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-                     "const char[] = R\"(\n"<br>
-                     "#include \"b.h\"\n"<br>
-                     ")\";\n";</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  std::string Expected = "#include \"a.h\"\n"<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-                         "#include \"c.h\"\n"<br>
-                         "const char[] = R\"(\n"<br>
-                         "#include \"b.h\"\n"<br>
-                         ")\";\n";</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  tooling::Replacements Replaces =<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-      toReplacements({createInsertion("#include \"c.h\"")});</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-TEST_F(CleanUpReplacementsTest, NoInsertionAfterOtherDirective) {</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  std::string Code = "#include \"a.h\"\n"<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-                     "#ifdef X\n"<br>
-                     "#include \"b.h\"\n"<br>
-                     "#endif\n";</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  std::string Expected = "#include \"a.h\"\n"<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-                         "#include \"c.h\"\n"<br>
-                         "#ifdef X\n"<br>
-                         "#include \"b.h\"\n"<br>
-                         "#endif\n";<br>
-  tooling::Replacements Replaces = toReplacements(<br>
-      {createInsertion("#include \"c.h\"")});</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-TEST_F(CleanUpReplacementsTest, CanInsertAfterLongSystemInclude) {</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  std::string Code = "#include \"a.h\"\n"<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-                     "// comment\n\n"<br>
-                     "#include <a/b/c/d/e.h>\n";</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  std::string Expected = "#include \"a.h\"\n"<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-                         "// comment\n\n"<br>
-                         "#include <a/b/c/d/e.h>\n"<br>
-                         "#include <x.h>\n";</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  tooling::Replacements Replaces =<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-      toReplacements({createInsertion("#include <x.h>")});</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-TEST_F(CleanUpReplacementsTest, CanInsertAfterComment) {</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  std::string Code = "#include \"a.h\"\n"<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-                     "// Comment\n"<br>
-                     "\n"<br>
-                     "/* Comment */\n"<br>
-                     "// Comment\n"<br>
-                     "\n"<br>
-                     "#include \"b.h\"\n";</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  std::string Expected = "#include \"a.h\"\n"<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-                         "// Comment\n"<br>
-                         "\n"<br>
-                         "/* Comment */\n"<br>
-                         "// Comment\n"<br>
-                         "\n"</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-                         "#include \"b.h\"\n"<br>
-                         "#include \"c.h\"\n";<br>
-  tooling::Replacements Replaces =<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-      toReplacements({createInsertion("#include \"c.h\"")});</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-TEST_F(CleanUpReplacementsTest, LongCommentsInTheBeginningOfFile) {<br>
-  std::string Code = "// Loooooooooooooooooooooooooong comment\n"<br>
-                     "// Loooooooooooooooooooooooooong comment\n"<br>
-                     "// Loooooooooooooooooooooooooong comment\n"<br>
-                     "#include <string>\n"</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-                     "#include <vector>\n"<br>
-                     "\n"<br>
-                     "#include \"a.h\"\n"<br>
-                     "#include \"b.h\"\n";<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-  std::string Expected = "// Loooooooooooooooooooooooooong comment\n"<br>
-                         "// Loooooooooooooooooooooooooong comment\n"<br>
-                         "// Loooooooooooooooooooooooooong comment\n"<br>
-                         "#include <string>\n"</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-                         "#include <vector>\n"<br>
-                         "\n"<br>
-                         "#include \"a.h\"\n"<br>
-                         "#include \"b.h\"\n"<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-                         "#include \"third.h\"\n";</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  tooling::Replacements Replaces =<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-      toReplacements({createInsertion("#include \"third.h\"")});</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);<br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-TEST_F(CleanUpReplacementsTest, CanDeleteAfterCode) {</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  std::string Code = "#include \"a.h\"\n"<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-                     "void f() {}\n"<br>
-                     "#include \"b.h\"\n";</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  std::string Expected = "#include \"a.h\"\n"<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-                         "void f() {}\n";<br>
-  tooling::Replacements Replaces = toReplacements({createDeletion("\"b.h\"")});</blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
-  EXPECT_EQ(Expected, apply(Code, Replaces));<br>
-}<br>
-<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
 } // end namespace<br>
 } // end namespace format<br>
 } // end namespace clang<br>
<br>
Modified: cfe/trunk/unittests/Tooling/CMakeLists.txt<br>
URL: <a href="<a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/CMakeLists.txt?rev=332287&r1=332286&r2=332287&view=diff" target="_blank" rel="noreferrer">http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/CMakeLists.txt?rev=332287&r1=332286&r2=332287&view=diff</a>" rel="noreferrer" targe</blockquote></div></div></blockquote></div></blockquote></div>