All defines in a RuleEntry are now added as Swift define flags to all indexer
targets to fix issues with indexing/code completion in blocks of code enclosed
in Swift defines.

PiperOrigin-RevId: 202499402
diff --git a/src/TulsiGenerator/Bazel/tulsi/tulsi_aspects.bzl b/src/TulsiGenerator/Bazel/tulsi/tulsi_aspects.bzl
index 4aa4514..7802138 100644
--- a/src/TulsiGenerator/Bazel/tulsi/tulsi_aspects.bzl
+++ b/src/TulsiGenerator/Bazel/tulsi/tulsi_aspects.bzl
@@ -691,22 +691,26 @@
     elif LegacySwiftInfo in target:
         swift_info = target[LegacySwiftInfo]
 
+    swift_defines = []
+
     if swift_info:
         attributes["has_swift_info"] = True
         transitive_attributes["swift_language_version"] = swift_info.swift_version
         transitive_attributes["has_swift_dependency"] = True
+        swift_defines = swift_info.transitive_defines.to_list()
 
     all_attributes = attributes + inheritable_attributes + transitive_attributes
 
     objc_provider = _get_opt_attr(target, "objc")
+    objc_defines = []
     target_includes = []
-    target_defines = []
+
     if objc_provider:
         target_includes = [
             _convert_outpath_to_symlink_path(x)
             for x in objc_provider.include
         ]
-        target_defines = objc_provider.define.to_list()
+        objc_defines = objc_provider.define.to_list()
 
     platform_type, os_deployment_target = _get_deployment_info(target, ctx)
     non_arc_srcs = _collect_files(rule, "attr.non_arc_srcs")
@@ -729,7 +733,8 @@
         build_file = ctx.build_file_path,
         bundle_id = bundle_id,
         bundle_name = bundle_name,
-        defines = target_defines,
+        objc_defines = objc_defines,
+        swift_defines = swift_defines,
         deps = compile_deps,
         extensions = extensions,
         framework_imports = _collect_framework_imports(rule_attr),
diff --git a/src/TulsiGenerator/BazelAspectInfoExtractor.swift b/src/TulsiGenerator/BazelAspectInfoExtractor.swift
index f015136..6a8e015 100644
--- a/src/TulsiGenerator/BazelAspectInfoExtractor.swift
+++ b/src/TulsiGenerator/BazelAspectInfoExtractor.swift
@@ -324,7 +324,8 @@
       } else {
         includePaths = nil
       }
-      let defines = dict["defines"] as? [String]
+      let objcDefines = dict["objc_defines"] as? [String]
+      let swiftDefines = dict["swift_defines"] as? [String]
       let deps = dict["deps"] as? [String] ?? []
       let dependencyLabels = Set(deps.map({ BuildLabel($0) }))
       let frameworkImports = MakeBazelFileInfos("framework_imports")
@@ -395,7 +396,8 @@
                                 platformType: platformType,
                                 osDeploymentTarget: osDeploymentTarget,
                                 buildFilePath: buildFilePath,
-                                defines: defines,
+                                objcDefines: objcDefines,
+                                swiftDefines: swiftDefines,
                                 includePaths: includePaths,
                                 swiftLanguageVersion: swiftLanguageVersion,
                                 swiftToolchain: swiftToolchain,
diff --git a/src/TulsiGenerator/PBXTargetGenerator.swift b/src/TulsiGenerator/PBXTargetGenerator.swift
index 836c445..a1d9199 100644
--- a/src/TulsiGenerator/PBXTargetGenerator.swift
+++ b/src/TulsiGenerator/PBXTargetGenerator.swift
@@ -511,7 +511,7 @@
         frameworkSearchPaths.union(inheritedFrameworkSearchPaths)
       }
       var defines = Set<String>()
-      if let ruleDefines = ruleEntry.defines {
+      if let ruleDefines = ruleEntry.objcDefines {
         defines.formUnion(ruleDefines)
       }
 
@@ -1217,6 +1217,12 @@
     swiftFlags.addObjects(from: ruleEntry.objCModuleMaps.map() {
       "-Xcc -fmodule-map-file=$(\(PBXTargetGenerator.BazelWorkspaceSymlinkVarName))/\($0.fullPath)"
     })
+
+    if let swiftDefines = ruleEntry.swiftDefines {
+      for flag in swiftDefines {
+        swiftFlags.add("-D\(flag)")
+      }
+    }
   }
 
   /// Reads the RuleEntry's copts and puts the arguments into the correct set.
diff --git a/src/TulsiGenerator/RuleEntry.swift b/src/TulsiGenerator/RuleEntry.swift
index 0d21ca4..6a98a84 100644
--- a/src/TulsiGenerator/RuleEntry.swift
+++ b/src/TulsiGenerator/RuleEntry.swift
@@ -179,8 +179,11 @@
   /// Artifacts produced by Bazel when this rule is built.
   public let artifacts: [BazelFileInfo]
 
-  /// Defines to be applied to this rule by Bazel.
-  public let defines: [String]?
+  /// Objective-C defines to be applied to this rule by Bazel.
+  public let objcDefines: [String]?
+
+  /// Swift defines to be applied to this rule by Bazel.
+  public let swiftDefines: [String]?
 
   /// Source files associated with this rule.
   public let sourceFiles: [BazelFileInfo]
@@ -326,7 +329,8 @@
        platformType: String? = nil,
        osDeploymentTarget: String? = nil,
        buildFilePath: String? = nil,
-       defines: [String]? = nil,
+       objcDefines: [String]? = nil,
+       swiftDefines: [String]? = nil,
        includePaths: [IncludePath]? = nil,
        swiftLanguageVersion: String? = nil,
        swiftToolchain: String? = nil,
@@ -376,7 +380,8 @@
     }
     self.deploymentTarget = deploymentTarget
     self.buildFilePath = buildFilePath
-    self.defines = defines
+    self.objcDefines = objcDefines
+    self.swiftDefines = swiftDefines
     self.includePaths = includePaths
     self.swiftLanguageVersion = swiftLanguageVersion
     self.swiftToolchain = swiftToolchain
@@ -420,7 +425,8 @@
                    platformType: String? = nil,
                    osDeploymentTarget: String? = nil,
                    buildFilePath: String? = nil,
-                   defines: [String]? = nil,
+                   objcDefines: [String]? = nil,
+                   swiftDefines: [String]? = nil,
                    includePaths: [IncludePath]? = nil,
                    swiftLanguageVersion: String? = nil,
                    swiftToolchain: String? = nil,
@@ -445,7 +451,8 @@
               platformType: platformType,
               osDeploymentTarget: osDeploymentTarget,
               buildFilePath: buildFilePath,
-              defines: defines,
+              objcDefines: objcDefines,
+              swiftDefines: swiftDefines,
               includePaths: includePaths,
               swiftLanguageVersion: swiftLanguageVersion,
               swiftToolchain: swiftToolchain,
diff --git a/src/TulsiGeneratorIntegrationTests/AspectTests.swift b/src/TulsiGeneratorIntegrationTests/AspectTests.swift
index 56f3391..24d206d 100644
--- a/src/TulsiGeneratorIntegrationTests/AspectTests.swift
+++ b/src/TulsiGeneratorIntegrationTests/AspectTests.swift
@@ -64,9 +64,9 @@
                                            ["is_dir": false,
                                             "path": "tulsi_test/SimpleTest.xcdatamodeld/SimpleDataModelsTestv2.xcdatamodel",
                                             "src": true], ] as NSArray)
-        .hasDefines(["LIBRARY_DEFINES_DEFINE=1",
-                     "APPLIB_ADDITIONAL_DEFINE",
-                     "APPLIB_ANOTHER_DEFINE=2"])
+        .hasObjcDefines(["LIBRARY_DEFINES_DEFINE=1",
+                         "APPLIB_ADDITIONAL_DEFINE",
+                         "APPLIB_ANOTHER_DEFINE=2"])
         .hasIncludes(["tulsi_test/ApplicationLibrary/includes",
                       "_tulsi-includes/x/x/tulsi_test/ApplicationLibrary/includes"])
         .hasAttribute(.supporting_files,
@@ -88,7 +88,7 @@
         .hasAttribute(.copts, value: ["-DLIBRARY_COPT_DEFINE",
                                       "-I/Library/absolute/include/path",
                                       "-Irelative/Library/include/path"] as NSArray)
-        .hasDefines(["LIBRARY_DEFINES_DEFINE=1"])
+        .hasObjcDefines(["LIBRARY_DEFINES_DEFINE=1"])
         .hasAttribute(.pch, value: ["is_dir": false,
                                     "path": "tulsi_test/Library/pch/PCHFile.pch",
                                     "src": true] as NSDictionary)
@@ -172,13 +172,13 @@
                      "bazel-genfiles/tulsi_test/SrcGenerator/outs/output.m"
                     ])
         .hasNonARCSources(["tulsi_test/Application/non_arc_srcs/NonARCFile.mm"])
-        .hasDefines(["SubLibraryWithDefines=1",
-                     "SubLibraryWithDefines_DEFINE=SubLibraryWithDefines",
-                     "SubLibraryWithDifferentDefines=1",
-                     "LIBRARY_DEFINES_DEFINE=1",
-                     "LIBRARY SECOND DEFINE=2",
-                     "LIBRARY_VALUE_WITH_SPACES=Value with spaces",
-                     "A=BINARY_DEFINE"])
+        .hasObjcDefines(["SubLibraryWithDefines=1",
+                         "SubLibraryWithDefines_DEFINE=SubLibraryWithDefines",
+                         "SubLibraryWithDifferentDefines=1",
+                         "LIBRARY_DEFINES_DEFINE=1",
+                         "LIBRARY SECOND DEFINE=2",
+                         "LIBRARY_VALUE_WITH_SPACES=Value with spaces",
+                         "A=BINARY_DEFINE"])
         .hasIncludes(["tulsi_test/Application/includes/first/include",
                       "tulsi-includes/x/x/tulsi_test/Application/includes/first/include",
                       "tulsi_test/Application/includes/second/include",
@@ -242,12 +242,12 @@
                      "tulsi_test/Library/srcs/SrcsHeader.h",
                      "tulsi_test/Library/hdrs/HdrsHeader.h"])
         .hasAttribute(.copts, value: ["-DLIBRARY_COPT_DEFINE"] as NSArray)
-        .hasDefines(["SubLibraryWithDefines=1",
-                     "SubLibraryWithDefines_DEFINE=SubLibraryWithDefines",
-                     "SubLibraryWithDifferentDefines=1",
-                     "LIBRARY_DEFINES_DEFINE=1",
-                     "LIBRARY SECOND DEFINE=2",
-                     "LIBRARY_VALUE_WITH_SPACES=Value with spaces",])
+        .hasObjcDefines(["SubLibraryWithDefines=1",
+                         "SubLibraryWithDefines_DEFINE=SubLibraryWithDefines",
+                         "SubLibraryWithDifferentDefines=1",
+                         "LIBRARY_DEFINES_DEFINE=1",
+                         "LIBRARY SECOND DEFINE=2",
+                         "LIBRARY_VALUE_WITH_SPACES=Value with spaces",])
         .hasAttribute(.pch, value: ["is_dir": false,
                                     "path": "tulsi_test/PCHGenerator/outs/PCHFile.pch",
                                     "root": "bazel-genfiles",
@@ -268,8 +268,8 @@
                                       "-menable-no-infs",
                                       "-I/SubLibraryWithDefines/local/includes",
                                       "-Irelative/SubLibraryWithDefines/local/includes"] as NSArray)
-        .hasDefines(["SubLibraryWithDefines=1",
-                     "SubLibraryWithDefines_DEFINE=SubLibraryWithDefines"])
+        .hasObjcDefines(["SubLibraryWithDefines=1",
+                         "SubLibraryWithDefines_DEFINE=SubLibraryWithDefines"])
 
     checker.assertThat("//tulsi_test:SubLibraryWithDifferentDefines")
         .hasSources(["tulsi_test/SubLibraryWithDifferentDefines/srcs/src.mm"])
@@ -279,7 +279,7 @@
                                       "-DSubLibraryWithDifferentDefines_STRING_WITH_SPACES='String with spaces'",
                                       "-D'SubLibraryWithDifferentDefines Define with spaces'",
                                       "-D'SubLibraryWithDifferentDefines Define with spaces and value'=1"] as NSArray)
-        .hasDefines(["SubLibraryWithDifferentDefines=1"])
+        .hasObjcDefines(["SubLibraryWithDifferentDefines=1"])
         .hasIncludes(["tulsi_test/SubLibraryWithDifferentDefines/includes",
                       "tulsi-includes/x/x/tulsi_test/SubLibraryWithDifferentDefines/includes"])
 
@@ -426,8 +426,11 @@
     checker.assertThat("//tulsi_test:SwiftLibrary")
         .hasSources(["tulsi_test/SwiftLibrary/srcs/a.swift",
                      "tulsi_test/SwiftLibrary/srcs/b.swift"])
+        .dependsOn("//tulsi_test:SubSwiftLibrary")
         .hasAttribute(.has_swift_dependency, value: true)
         .hasAttribute(.has_swift_info, value: true)
+        .hasSwiftDefines(["SUB_LIBRARY_DEFINE",
+                          "LIBRARY_DEFINE"])
 
     checker.assertThat("//tulsi_test:SwiftLibraryV3")
         .hasSources(["tulsi_test/SwiftLibraryV3/srcs/a.swift",
@@ -435,6 +438,7 @@
         .hasAttribute(.swift_language_version, value: "3")
         .hasAttribute(.has_swift_dependency, value: true)
         .hasAttribute(.has_swift_info, value: true)
+        .hasSwiftDefines(["LIBRARY_DEFINE_V3"])
 
     checker.assertThat("//tulsi_test:SwiftLibraryV4")
         .hasSources(["tulsi_test/SwiftLibraryV4/srcs/a.swift",
@@ -442,6 +446,7 @@
         .hasAttribute(.swift_language_version, value: "4")
         .hasAttribute(.has_swift_dependency, value: true)
         .hasAttribute(.has_swift_info, value: true)
+        .hasSwiftDefines(["LIBRARY_DEFINE_V4"])
   }
 
 }
@@ -700,9 +705,21 @@
 
     /// Asserts that the contextual RuleEntry has an attribute with the given name and value.
     @discardableResult
-    func hasDefines(_ value: [String], line: UInt = #line) -> Context {
+    func hasObjcDefines(_ value: [String], line: UInt = #line) -> Context {
       guard let ruleEntry = ruleEntry else { return self }
-      guard let defines = ruleEntry.defines else {
+      guard let defines = ruleEntry.objcDefines else {
+        XCTFail("\(ruleEntry) expected to have defines", line: line)
+        return self
+      }
+      XCTAssertEqual(defines, value, line: line)
+      return self
+    }
+
+    /// Asserts that the contextual RuleEntry has an attribute with the given name and value.
+    @discardableResult
+    func hasSwiftDefines(_ value: [String], line: UInt = #line) -> Context {
+      guard let ruleEntry = ruleEntry else { return self }
+      guard let defines = ruleEntry.swiftDefines else {
         XCTFail("\(ruleEntry) expected to have defines", line: line)
         return self
       }
diff --git a/src/TulsiGeneratorIntegrationTests/Resources/Swift.BUILD b/src/TulsiGeneratorIntegrationTests/Resources/Swift.BUILD
index 9ca183b..bf000b4 100644
--- a/src/TulsiGeneratorIntegrationTests/Resources/Swift.BUILD
+++ b/src/TulsiGeneratorIntegrationTests/Resources/Swift.BUILD
@@ -54,6 +54,9 @@
         "-swift-version",
         "3",
     ],
+    defines = [
+        "LIBRARY_DEFINE_V3",
+    ],
 )
 
 swift_library(
@@ -66,6 +69,9 @@
         "-swift-version",
         "4",
     ],
+    defines = [
+        "LIBRARY_DEFINE_V4",
+    ],
 )
 
 swift_library(
@@ -74,4 +80,17 @@
         "SwiftLibrary/srcs/a.swift",
         "SwiftLibrary/srcs/b.swift",
     ],
+    defines = [
+        "LIBRARY_DEFINE",
+    ],
+    deps = [
+        ":SubSwiftLibrary",
+    ],
+)
+
+swift_library(
+    name = "SubSwiftLibrary",
+    defines = [
+        "SUB_LIBRARY_DEFINE",
+    ],
 )
diff --git a/src/TulsiGeneratorTests/XcodeProjectGeneratorTests.swift b/src/TulsiGeneratorTests/XcodeProjectGeneratorTests.swift
index 087487d..69f46ce 100644
--- a/src/TulsiGeneratorTests/XcodeProjectGeneratorTests.swift
+++ b/src/TulsiGeneratorTests/XcodeProjectGeneratorTests.swift
@@ -275,7 +275,8 @@
                                     secondaryArtifacts: [BazelFileInfo] = [],
                                     weakDependencies: Set<BuildLabel>? = nil,
                                     buildFilePath: String? = nil,
-                                    defines: [String]? = nil,
+                                    objcDefines: [String]? = nil,
+                                    swiftDefines: [String]? = nil,
                                     includePaths: [RuleEntry.IncludePath]? = nil,
                                     extensions: Set<BuildLabel>? = nil,
                                     productType: PBXTarget.ProductType? = nil,
@@ -296,7 +297,8 @@
                      platformType: platformType,
                      osDeploymentTarget: osDeploymentTarget,
                      buildFilePath: buildFilePath,
-                     defines: defines,
+                     objcDefines: objcDefines,
+                     swiftDefines: swiftDefines,
                      includePaths: includePaths,
                      extensionType: extensionType)
   }