[clang-tools-extra] r268017 - [include-fixer] Add Yaml database integration.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 29 02:23:38 PDT 2016


Author: hokein
Date: Fri Apr 29 04:23:38 2016
New Revision: 268017

URL: http://llvm.org/viewvc/llvm-project?rev=268017&view=rev
Log:
[include-fixer] Add Yaml database integration.

Reviewers: bkramer

Subscribers: cfe-commits, klimek, djasper

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

Added:
    clang-tools-extra/trunk/include-fixer/YamlXrefsDB.cpp
    clang-tools-extra/trunk/include-fixer/YamlXrefsDB.h
    clang-tools-extra/trunk/test/include-fixer/Inputs/
    clang-tools-extra/trunk/test/include-fixer/Inputs/fake_yaml_db.yaml
    clang-tools-extra/trunk/test/include-fixer/yamldb.cpp
Modified:
    clang-tools-extra/trunk/include-fixer/CMakeLists.txt
    clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp

Modified: clang-tools-extra/trunk/include-fixer/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/CMakeLists.txt?rev=268017&r1=268016&r2=268017&view=diff
==============================================================================
--- clang-tools-extra/trunk/include-fixer/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/include-fixer/CMakeLists.txt Fri Apr 29 04:23:38 2016
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
 add_clang_library(clangIncludeFixer
   IncludeFixer.cpp
   InMemoryXrefsDB.cpp
+  YamlXrefsDB.cpp
 
   LINK_LIBS
   clangAST
@@ -15,6 +16,7 @@ add_clang_library(clangIncludeFixer
   clangSema
   clangTooling
   clangToolingCore
+  findAllSymbols
   )
 
 add_subdirectory(tool)

Added: clang-tools-extra/trunk/include-fixer/YamlXrefsDB.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/YamlXrefsDB.cpp?rev=268017&view=auto
==============================================================================
--- clang-tools-extra/trunk/include-fixer/YamlXrefsDB.cpp (added)
+++ clang-tools-extra/trunk/include-fixer/YamlXrefsDB.cpp Fri Apr 29 04:23:38 2016
@@ -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

Added: clang-tools-extra/trunk/include-fixer/YamlXrefsDB.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/YamlXrefsDB.h?rev=268017&view=auto
==============================================================================
--- clang-tools-extra/trunk/include-fixer/YamlXrefsDB.h (added)
+++ clang-tools-extra/trunk/include-fixer/YamlXrefsDB.h Fri Apr 29 04:23:38 2016
@@ -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

Modified: clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp?rev=268017&r1=268016&r2=268017&view=diff
==============================================================================
--- clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp (original)
+++ clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp Fri Apr 29 04:23:38 2016
@@ -9,11 +9,13 @@
 
 #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;
 
@@ -22,11 +24,14 @@ cl::OptionCategory IncludeFixerCategory(
 
 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",
@@ -67,6 +72,10 @@ int main(int argc, const char **argv) {
         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.

Added: clang-tools-extra/trunk/test/include-fixer/Inputs/fake_yaml_db.yaml
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/include-fixer/Inputs/fake_yaml_db.yaml?rev=268017&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/include-fixer/Inputs/fake_yaml_db.yaml (added)
+++ clang-tools-extra/trunk/test/include-fixer/Inputs/fake_yaml_db.yaml Fri Apr 29 04:23:38 2016
@@ -0,0 +1,11 @@
+---
+Name:           foo
+Contexts:
+  - ContextType:     Namespace
+    ContextName:     a
+  - ContextType:     Namespace
+    ContextName:     b
+FilePath:        foo.h
+LineNumber:      1
+Type:            Class
+...

Added: clang-tools-extra/trunk/test/include-fixer/yamldb.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/include-fixer/yamldb.cpp?rev=268017&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/include-fixer/yamldb.cpp (added)
+++ clang-tools-extra/trunk/test/include-fixer/yamldb.cpp Fri Apr 29 04:23:38 2016
@@ -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;




More information about the cfe-commits mailing list