[include-fixer] only deduplicate symbols after matching symbols with the undefined identifier.

Summary:
we should only deduplicate symbols after matching symbols with the
undefined identifier. This patch tries to fix the situation where matching
symbols are deleted when it has the same header as a non-matching symbol, which
can prevent finding the correct header even if there is a matched symbol.

Reviewers: bkramer

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D21290

llvm-svn: 272576
diff --git a/clang-tools-extra/include-fixer/SymbolIndexManager.cpp b/clang-tools-extra/include-fixer/SymbolIndexManager.cpp
index b3ecca4..ae9faa2 100644
--- a/clang-tools-extra/include-fixer/SymbolIndexManager.cpp
+++ b/clang-tools-extra/include-fixer/SymbolIndexManager.cpp
@@ -67,8 +67,8 @@
   // Eventually we will either hit a class (namespaces aren't in the database
   // either) and can report that result.
   bool TookPrefix = false;
-  std::vector<std::string> Results;
-  while (Results.empty() && !Names.empty()) {
+  std::vector<clang::find_all_symbols::SymbolInfo> MatchedSymbols;
+  while (MatchedSymbols.empty() && !Names.empty()) {
     std::vector<clang::find_all_symbols::SymbolInfo> Symbols;
     for (const auto &DB : SymbolIndices) {
       auto Res = DB->search(Names.back().str());
@@ -78,8 +78,6 @@
     DEBUG(llvm::dbgs() << "Searching " << Names.back() << "... got "
                        << Symbols.size() << " results...\n");
 
-    rankByPopularity(Symbols);
-
     for (const auto &Symbol : Symbols) {
       // Match the identifier name without qualifier.
       if (Symbol.getName() == Names.back()) {
@@ -120,15 +118,7 @@
                Symbol.getSymbolKind() == SymbolInfo::SymbolKind::Macro))
             continue;
 
-          // FIXME: file path should never be in the form of <...> or "...", but
-          // the unit test with fixed database use <...> file path, which might
-          // need to be changed.
-          // FIXME: if the file path is a system header name, we want to use
-          // angle brackets.
-          std::string FilePath = Symbol.getFilePath().str();
-          Results.push_back((FilePath[0] == '"' || FilePath[0] == '<')
-                                ? FilePath
-                                : "\"" + FilePath + "\"");
+          MatchedSymbols.push_back(Symbol);
         }
       }
     }
@@ -136,6 +126,20 @@
     TookPrefix = true;
   }
 
+  rankByPopularity(MatchedSymbols);
+  std::vector<std::string> Results;
+  for (const auto &Symbol : MatchedSymbols) {
+    // FIXME: file path should never be in the form of <...> or "...", but
+    // the unit test with fixed database use <...> file path, which might
+    // need to be changed.
+    // FIXME: if the file path is a system header name, we want to use
+    // angle brackets.
+    std::string FilePath = Symbol.getFilePath().str();
+    Results.push_back((FilePath[0] == '"' || FilePath[0] == '<')
+                          ? FilePath
+                          : "\"" + FilePath + "\"");
+  }
+
   return Results;
 }