[Refactor Xcode Objects] Decouple file references and indexing target.

Previously, whenever a file reference needs to be added for indexing, a default
target with the same name as the project is always assumed to be the one to
bound to.

This CL decouples file references from the default indexing target, and allows
file references to be bound to any of the native targets.

BUG=614818

Review-Url: https://codereview.chromium.org/2577753002
Cr-Original-Commit-Position: refs/heads/master@{#439894}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 898a56b7966c5686d276b6aa7f27ec922b37ab38
diff --git a/tools/gn/xcode_object.cc b/tools/gn/xcode_object.cc
index 039cb52..e97cbfd 100644
--- a/tools/gn/xcode_object.cc
+++ b/tools/gn/xcode_object.cc
@@ -612,33 +612,26 @@
 
 PBXProject::~PBXProject() {}
 
+void PBXProject::AddSourceFileToIndexingTarget(
+    const std::string& navigator_path,
+    const std::string& source_path) {
+  if (!target_for_indexing_) {
+    AddIndexingTarget();
+  }
+  AddSourceFile(navigator_path, source_path, target_for_indexing_);
+}
+
 void PBXProject::AddSourceFile(const std::string& navigator_path,
-                               const std::string& source_path) {
+                               const std::string& source_path,
+                               PBXNativeTarget* target) {
   PBXFileReference* file_reference =
       sources_->AddSourceFile(navigator_path, source_path);
   base::StringPiece ext = FindExtension(&source_path);
   if (!IsSourceFileForIndexing(ext))
     return;
 
-  if (!target_for_indexing_) {
-    PBXAttributes attributes;
-    attributes["EXECUTABLE_PREFIX"] = "";
-    attributes["HEADER_SEARCH_PATHS"] = sources_->path();
-    attributes["PRODUCT_NAME"] = name_;
-
-    PBXFileReference* product_reference = static_cast<PBXFileReference*>(
-        products_->AddChild(base::MakeUnique<PBXFileReference>(
-            std::string(), name_, "compiled.mach-o.executable")));
-
-    const char product_type[] = "com.apple.product-type.tool";
-    targets_.push_back(base::MakeUnique<PBXNativeTarget>(
-        name_, std::string(), config_name_, attributes, product_type, name_,
-        product_reference));
-    target_for_indexing_ = static_cast<PBXNativeTarget*>(targets_.back().get());
-  }
-
-  DCHECK(target_for_indexing_);
-  target_for_indexing_->AddFileForIndexing(file_reference);
+  DCHECK(target);
+  target->AddFileForIndexing(file_reference);
 }
 
 void PBXProject::AddAggregateTarget(const std::string& name,
@@ -652,6 +645,24 @@
       name, shell_script, config_name_, attributes));
 }
 
+void PBXProject::AddIndexingTarget() {
+  DCHECK(!target_for_indexing_);
+  PBXAttributes attributes;
+  attributes["EXECUTABLE_PREFIX"] = "";
+  attributes["HEADER_SEARCH_PATHS"] = sources_->path();
+  attributes["PRODUCT_NAME"] = name_;
+
+  PBXFileReference* product_reference = static_cast<PBXFileReference*>(
+      products_->AddChild(base::MakeUnique<PBXFileReference>(
+          std::string(), name_, "compiled.mach-o.executable")));
+
+  const char product_type[] = "com.apple.product-type.tool";
+  targets_.push_back(base::MakeUnique<PBXNativeTarget>(
+      name_, std::string(), config_name_, attributes, product_type, name_,
+      product_reference));
+  target_for_indexing_ = static_cast<PBXNativeTarget*>(targets_.back().get());
+}
+
 void PBXProject::AddNativeTarget(const std::string& name,
                                  const std::string& type,
                                  const std::string& output_name,
diff --git a/tools/gn/xcode_object.h b/tools/gn/xcode_object.h
index 83a776a..f56de09 100644
--- a/tools/gn/xcode_object.h
+++ b/tools/gn/xcode_object.h
@@ -275,10 +275,15 @@
              const PBXAttributes& attributes);
   ~PBXProject() override;
 
+  void AddSourceFileToIndexingTarget(const std::string& navigator_path,
+                                     const std::string& source_path);
   void AddSourceFile(const std::string& navigator_path,
-                     const std::string& source_path);
+                     const std::string& source_path,
+                     PBXNativeTarget* target);
+
   void AddAggregateTarget(const std::string& name,
                           const std::string& shell_script);
+  void AddIndexingTarget();
   void AddNativeTarget(const std::string& name,
                        const std::string& type,
                        const std::string& output_name,
diff --git a/tools/gn/xcode_writer.cc b/tools/gn/xcode_writer.cc
index 07b50e4..b052683 100644
--- a/tools/gn/xcode_writer.cc
+++ b/tools/gn/xcode_writer.cc
@@ -353,7 +353,8 @@
   for (const SourceFile& source : sources) {
     std::string source_file =
         RebasePath(source.value(), source_dir, absolute_source_path);
-    sources_for_indexing->AddSourceFile(source_file, source_file);
+    sources_for_indexing->AddSourceFileToIndexingTarget(source_file,
+                                                        source_file);
   }
 
   projects_.push_back(std::move(sources_for_indexing));