hokein updated this revision to Diff 55412.
hokein added a comment.

Fix a nit.


http://reviews.llvm.org/D19648

Files:
  include-fixer/CMakeLists.txt
  include-fixer/YamlXrefsDB.cpp
  include-fixer/YamlXrefsDB.h
  include-fixer/tool/ClangIncludeFixer.cpp
  test/include-fixer/Inputs/fake_yaml_db.yaml
  test/include-fixer/yamldb.cpp

Index: test/include-fixer/yamldb.cpp
===================================================================
--- /dev/null
+++ test/include-fixer/yamldb.cpp
@@ -0,0 +1,9 @@
+// REQUIRES: shell
+// RUN: sed -e 's#//.*$##' %s > %t.cpp
+// RUN: clang-include-fixer -db=yaml -input=%p/Inputs/fake_yaml_db.yaml %t.cpp --
+// RUN: FileCheck %s -input-file=%t.cpp
+
+// CHECK: #include "foo.h"
+// CHECK: b::a::foo f;
+
+b::a::foo f;
Index: test/include-fixer/Inputs/fake_yaml_db.yaml
===================================================================
--- /dev/null
+++ test/include-fixer/Inputs/fake_yaml_db.yaml
@@ -0,0 +1,11 @@
+---
+Name:           foo
+Contexts:
+  - ContextType:     Namespace
+    ContextName:     a
+  - ContextType:     Namespace
+    ContextName:     b
+FilePath:        foo.h
+LineNumber:      1
+Type:            Class
+...
Index: include-fixer/tool/ClangIncludeFixer.cpp
===================================================================
--- include-fixer/tool/ClangIncludeFixer.cpp
+++ include-fixer/tool/ClangIncludeFixer.cpp
@@ -9,24 +9,29 @@
 
 #include "InMemoryXrefsDB.h"
 #include "IncludeFixer.h"
+#include "YamlXrefsDB.h"
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Rewrite/Core/Rewriter.h"
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/Support/CommandLine.h"
+
 using namespace clang;
 using namespace llvm;
 
 namespace {
 cl::OptionCategory IncludeFixerCategory("Tool options");
 
 enum DatabaseFormatTy {
   fixed, //< Hard-coded mapping.
+  yaml,  //< Yaml database created by find-all-symbols.
 };
 
 cl::opt<DatabaseFormatTy> DatabaseFormat(
     "db", cl::desc("Specify input format"),
-    cl::values(clEnumVal(fixed, "Hard-coded mapping"), clEnumValEnd),
+    cl::values(clEnumVal(fixed, "Hard-coded mapping"),
+               clEnumVal(yaml, "Yaml database created by find-all-symbols"),
+               clEnumValEnd),
     cl::init(fixed), cl::cat(IncludeFixerCategory));
 
 cl::opt<std::string> Input("input",
@@ -62,6 +67,10 @@
         llvm::make_unique<include_fixer::InMemoryXrefsDB>(std::move(XrefsMap));
     break;
   }
+  case yaml: {
+    XrefsDB = llvm::make_unique<include_fixer::YamlXrefsDB>(Input);
+    break;
+  }
   }
 
   // Now run our tool.
Index: include-fixer/YamlXrefsDB.h
===================================================================
--- /dev/null
+++ include-fixer/YamlXrefsDB.h
@@ -0,0 +1,35 @@
+//===-- YamlXrefsDB.h -------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_YAMLXREFSDB_H
+#define LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_YAMLXREFSDB_H
+
+#include "XrefsDB.h"
+#include "find-all-symbols/SymbolInfo.h"
+#include <map>
+#include <vector>
+
+namespace clang {
+namespace include_fixer {
+
+/// Yaml format database.
+class YamlXrefsDB : public XrefsDB {
+public:
+  YamlXrefsDB(llvm::StringRef FilePath);
+
+  std::vector<std::string> search(llvm::StringRef Identifier) override;
+
+private:
+  std::vector<clang::find_all_symbols::SymbolInfo> Symbols;
+};
+
+} // namespace include_fixer
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_YAMLXREFSDB_H
Index: include-fixer/YamlXrefsDB.cpp
===================================================================
--- /dev/null
+++ include-fixer/YamlXrefsDB.cpp
@@ -0,0 +1,64 @@
+//===-- YamlXrefsDB.cpp ---------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "YamlXrefsDB.h"
+
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include <string>
+#include <vector>
+
+namespace clang {
+namespace include_fixer {
+
+YamlXrefsDB::YamlXrefsDB(llvm::StringRef FilePath) {
+  int ReadFD = 0;
+  if (llvm::sys::fs::openFileForRead(FilePath, ReadFD))
+    return;
+  auto Buffer = llvm::MemoryBuffer::getOpenFile(ReadFD, FilePath, -1);
+  if (!Buffer)
+    return;
+  Symbols = clang::find_all_symbols::ReadSymbolInfosFromYAML(
+      Buffer.get()->getBuffer());
+}
+
+std::vector<std::string> YamlXrefsDB::search(llvm::StringRef Identifier) {
+  llvm::SmallVector<llvm::StringRef, 16> Names;
+  std::vector<std::string> Results;
+
+  // The identifier may be fully qualified, so split it and get all the context
+  // names.
+  Identifier.split(Names, "::");
+  for (const auto &Symbol : Symbols) {
+    // Match the identifier name without qualifier.
+    if (Symbol.Name == Names.back()) {
+      bool IsMatched = true;
+      auto SymbolContext = Symbol.Contexts.begin();
+      // Match the remaining context names.
+      for (auto IdentiferContext = Names.rbegin() + 1;
+           IdentiferContext != Names.rend() &&
+           SymbolContext != Symbol.Contexts.end();
+           ++IdentiferContext, ++SymbolContext) {
+        if (SymbolContext->second != *IdentiferContext) {
+          IsMatched = false;
+          break;
+        }
+      }
+
+      if (IsMatched) {
+        Results.push_back("\"" + Symbol.FilePath + "\"");
+      }
+    }
+  }
+  return Results;
+}
+
+} // namespace include_fixer
+} // namespace clang
Index: include-fixer/CMakeLists.txt
===================================================================
--- include-fixer/CMakeLists.txt
+++ include-fixer/CMakeLists.txt
@@ -5,6 +5,7 @@
 add_clang_library(clangIncludeFixer
   IncludeFixer.cpp
   InMemoryXrefsDB.cpp
+  YamlXrefsDB.cpp
 
   LINK_LIBS
   clangAST
@@ -15,6 +16,7 @@
   clangSema
   clangTooling
   clangToolingCore
+  findAllSymbols
   )
 
 add_subdirectory(tool)
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to