[clang-tools-extra] r271273 - [include-fixer] Code cleanup.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Tue May 31 06:23:06 PDT 2016


Author: hokein
Date: Tue May 31 08:23:00 2016
New Revision: 271273

URL: http://llvm.org/viewvc/llvm-project?rev=271273&view=rev
Log:
[include-fixer] Code cleanup.

Summary:
* Abstract the DB setting code to a function.
* Remove the unused FallbackStyle.

Reviewers: bkramer

Subscribers: cfe-commits

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

Modified:
    clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
    clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp

Modified: clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp?rev=271273&r1=271272&r2=271273&view=diff
==============================================================================
--- clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp (original)
+++ clang-tools-extra/trunk/include-fixer/IncludeFixer.cpp Tue May 31 08:23:00 2016
@@ -59,9 +59,8 @@ private:
 class Action : public clang::ASTFrontendAction,
                public clang::ExternalSemaSource {
 public:
-  explicit Action(SymbolIndexManager &SymbolIndexMgr, StringRef StyleName,
-                  bool MinimizeIncludePaths)
-      : SymbolIndexMgr(SymbolIndexMgr), FallbackStyle(StyleName),
+  explicit Action(SymbolIndexManager &SymbolIndexMgr, bool MinimizeIncludePaths)
+      : SymbolIndexMgr(SymbolIndexMgr),
         MinimizeIncludePaths(MinimizeIncludePaths) {}
 
   std::unique_ptr<clang::ASTConsumer>
@@ -288,10 +287,6 @@ private:
   /// be used as the insertion point for new include directives.
   unsigned FirstIncludeOffset = -1U;
 
-  /// The fallback format style for formatting after insertion if there is no
-  /// clang-format config file found.
-  std::string FallbackStyle;
-
   /// The symbol being queried.
   std::string QuerySymbol;
 
@@ -347,7 +342,7 @@ IncludeFixerActionFactory::IncludeFixerA
     SymbolIndexManager &SymbolIndexMgr, IncludeFixerContext &Context,
     StringRef StyleName, bool MinimizeIncludePaths)
     : SymbolIndexMgr(SymbolIndexMgr), Context(Context),
-      MinimizeIncludePaths(MinimizeIncludePaths), FallbackStyle(StyleName) {}
+      MinimizeIncludePaths(MinimizeIncludePaths) {}
 
 IncludeFixerActionFactory::~IncludeFixerActionFactory() = default;
 
@@ -373,8 +368,8 @@ bool IncludeFixerActionFactory::runInvoc
   Compiler.getDiagnostics().setErrorLimit(0);
 
   // Run the parser, gather missing includes.
-  auto ScopedToolAction = llvm::make_unique<Action>(
-      SymbolIndexMgr, FallbackStyle, MinimizeIncludePaths);
+  auto ScopedToolAction =
+      llvm::make_unique<Action>(SymbolIndexMgr, MinimizeIncludePaths);
   Compiler.ExecuteAction(*ScopedToolAction);
 
   Context = ScopedToolAction->getIncludeFixerContext(

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=271273&r1=271272&r2=271273&view=diff
==============================================================================
--- clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp (original)
+++ clang-tools-extra/trunk/include-fixer/tool/ClangIncludeFixer.cpp Tue May 31 08:23:00 2016
@@ -81,6 +81,59 @@ cl::opt<std::string>
                    "headers if there is no clang-format config file found."),
           cl::init("llvm"), cl::cat(IncludeFixerCategory));
 
+std::unique_ptr<include_fixer::SymbolIndexManager>
+createSymbolIndexManager(StringRef FilePath) {
+  auto SymbolIndexMgr = llvm::make_unique<include_fixer::SymbolIndexManager>();
+  switch (DatabaseFormat) {
+  case fixed: {
+    // Parse input and fill the database with it.
+    // <symbol>=<header><, header...>
+    // Multiple symbols can be given, separated by semicolons.
+    std::map<std::string, std::vector<std::string>> SymbolsMap;
+    SmallVector<StringRef, 4> SemicolonSplits;
+    StringRef(Input).split(SemicolonSplits, ";");
+    std::vector<find_all_symbols::SymbolInfo> Symbols;
+    for (StringRef Pair : SemicolonSplits) {
+      auto Split = Pair.split('=');
+      std::vector<std::string> Headers;
+      SmallVector<StringRef, 4> CommaSplits;
+      Split.second.split(CommaSplits, ",");
+      for (StringRef Header : CommaSplits)
+        Symbols.push_back(find_all_symbols::SymbolInfo(
+            Split.first.trim(),
+            find_all_symbols::SymbolInfo::SymbolKind::Unknown, Header.trim(), 1,
+            {}));
+    }
+    SymbolIndexMgr->addSymbolIndex(
+        llvm::make_unique<include_fixer::InMemorySymbolIndex>(Symbols));
+    break;
+  }
+  case yaml: {
+    llvm::ErrorOr<std::unique_ptr<include_fixer::YamlSymbolIndex>> DB(nullptr);
+    if (!Input.empty()) {
+      DB = include_fixer::YamlSymbolIndex::createFromFile(Input);
+    } else {
+      // If we don't have any input file, look in the directory of the first
+      // file and its parents.
+      SmallString<128> AbsolutePath(tooling::getAbsolutePath(FilePath));
+      StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
+      DB = include_fixer::YamlSymbolIndex::createFromDirectory(
+          Directory, "find_all_symbols_db.yaml");
+    }
+
+    if (!DB) {
+      llvm::errs() << "Couldn't find YAML db: " << DB.getError().message()
+                   << '\n';
+      return nullptr;
+    }
+
+    SymbolIndexMgr->addSymbolIndex(std::move(*DB));
+    break;
+  }
+  }
+  return SymbolIndexMgr;
+}
+
 int includeFixerMain(int argc, const char **argv) {
   tooling::CommonOptionsParser options(argc, argv, IncludeFixerCategory);
   tooling::ClangTool tool(options.getCompilations(),
@@ -128,55 +181,10 @@ int includeFixerMain(int argc, const cha
   }
 
   // Set up data source.
-  auto SymbolIndexMgr = llvm::make_unique<include_fixer::SymbolIndexManager>();
-  switch (DatabaseFormat) {
-  case fixed: {
-    // Parse input and fill the database with it.
-    // <symbol>=<header><, header...>
-    // Multiple symbols can be given, separated by semicolons.
-    std::map<std::string, std::vector<std::string>> SymbolsMap;
-    SmallVector<StringRef, 4> SemicolonSplits;
-    StringRef(Input).split(SemicolonSplits, ";");
-    std::vector<find_all_symbols::SymbolInfo> Symbols;
-    for (StringRef Pair : SemicolonSplits) {
-      auto Split = Pair.split('=');
-      std::vector<std::string> Headers;
-      SmallVector<StringRef, 4> CommaSplits;
-      Split.second.split(CommaSplits, ",");
-      for (StringRef Header : CommaSplits)
-        Symbols.push_back(find_all_symbols::SymbolInfo(
-            Split.first.trim(),
-            find_all_symbols::SymbolInfo::SymbolKind::Unknown, Header.trim(), 1,
-            {}));
-    }
-    SymbolIndexMgr->addSymbolIndex(
-        llvm::make_unique<include_fixer::InMemorySymbolIndex>(Symbols));
-    break;
-  }
-  case yaml: {
-    llvm::ErrorOr<std::unique_ptr<include_fixer::YamlSymbolIndex>> DB(nullptr);
-    if (!Input.empty()) {
-      DB = include_fixer::YamlSymbolIndex::createFromFile(Input);
-    } else {
-      // If we don't have any input file, look in the directory of the first
-      // file and its parents.
-      SmallString<128> AbsolutePath(
-          tooling::getAbsolutePath(options.getSourcePathList().front()));
-      StringRef Directory = llvm::sys::path::parent_path(AbsolutePath);
-      DB = include_fixer::YamlSymbolIndex::createFromDirectory(
-          Directory, "find_all_symbols_db.yaml");
-    }
-
-    if (!DB) {
-      llvm::errs() << "Couldn't find YAML db: " << DB.getError().message()
-                   << '\n';
-      return 1;
-    }
-
-    SymbolIndexMgr->addSymbolIndex(std::move(*DB));
-    break;
-  }
-  }
+  std::unique_ptr<include_fixer::SymbolIndexManager> SymbolIndexMgr =
+      createSymbolIndexManager(options.getSourcePathList().front());
+  if (!SymbolIndexMgr)
+    return 1;
 
   // Now run our tool.
   include_fixer::IncludeFixerContext Context;




More information about the cfe-commits mailing list