Repository: bazelbuild/buildtools
Pull request #676 (2019-07-22 12:36:10)

Add native-cc fix to buildifier.


Patch:
diff --git a/WARNINGS.md b/WARNINGS.md
index b1cbc4b..bebe95d 100644
--- a/WARNINGS.md
+++ b/WARNINGS.md
@@ -30,6 +30,7 @@ Warning categories supported by buildifier's linter:
   * [name-conventions](#name-conventions)
   * [native-android](#native-android)
   * [native-build](#native-build)
+  * [native-cc](#native-cc)
   * [native-java](#native-java)
   * [native-package](#native-package)
   * [no-effect](#no-effect)
@@ -479,6 +480,17 @@ there.
 
 --------------------------------------------------------------------------------
 
+## <a name="native-cc"></a>All C++ build rules should be loaded from Starlark
+
+  * Category name: `native-cc`
+  * Flag in Bazel: [`--incompatible_load_cc_rules_from_bzl`](https://github.com/bazelbuild/bazel/issues/8743)
+  * Automatic fix: yes
+
+The C++ build rules should be loaded from Starlark. The native rules [will be
+disabled](https://github.com/bazelbuild/bazel/issues/8743).
+
+--------------------------------------------------------------------------------
+
 ## <a name="native-java"></a>All Java build rules should be loaded from Starlark
 
   * Category name: `native-java`
diff --git a/tables/tables.go b/tables/tables.go
index 6b47ad3..2dfe75a 100644
--- a/tables/tables.go
+++ b/tables/tables.go
@@ -223,6 +223,24 @@ var AndroidNativeRules = []string{
 // AndroidLoadPath is the load path for the Starlark Android Rules.
 var AndroidLoadPath = "@rules_android//android:rules.bzl"
 
+// CcNativeRules lists all C++ rules that are being migrated from Native to Starlark.
+var CcNativeRules = []string{
+	"cc_binary",
+	"cc_test",
+	"cc_library",
+	"cc_import",
+	"cc_proto_library",
+	"fdo_prefetch_hints",
+	"fdo_profile",
+	"cc_toolchain",
+	"cc_toolchain_suite",
+	"objc_library",
+	"objc_import",
+}
+
+// CcLoadPath is the load path for the Starlark C++ Rules.
+var CcLoadPath = "@rules_cc//cc:defs.bzl"
+
 // JavaNativeRules lists all Java rules that are being migrated from Native to Starlark.
 var JavaNativeRules = []string{
 	"java_binary",
diff --git a/warn/warn.go b/warn/warn.go
index fdc0137..b4fd56d 100644
--- a/warn/warn.go
+++ b/warn/warn.go
@@ -128,6 +128,7 @@ var FileWarningMap = map[string]func(f *build.File) []*LinterFinding{
 	"name-conventions":          nameConventionsWarning,
 	"native-android":            nativeAndroidRulesWarning,
 	"native-build":              nativeInBuildFilesWarning,
+	"native-cc":                 nativeCcRulesWarning,
 	"native-java":               nativeJavaRulesWarning,
 	"native-package":            nativePackageWarning,
 	"no-effect":                 noEffectWarning,
diff --git a/warn/warn_bazel_api.go b/warn/warn_bazel_api.go
index 3c4533d..d5bd901 100644
--- a/warn/warn_bazel_api.go
+++ b/warn/warn_bazel_api.go
@@ -578,6 +578,13 @@ func nativeAndroidRulesWarning(f *build.File) []*LinterFinding {
 	return notLoadedFunctionUsageCheck(f, tables.AndroidNativeRules, tables.AndroidLoadPath)
 }
 
+func nativeCcRulesWarning(f *build.File) []*LinterFinding {
+	if f.Type != build.TypeBzl && f.Type != build.TypeBuild {
+		return nil
+	}
+	return notLoadedFunctionUsageCheck(f, tables.CcNativeRules, tables.CcLoadPath)
+}
+
 func nativeJavaRulesWarning(f *build.File) []*LinterFinding {
 	if f.Type != build.TypeBzl && f.Type != build.TypeBuild {
 		return nil


Test patch:
diff --git a/warn/warn_bazel_api_test.go b/warn/warn_bazel_api_test.go
index 27088f3..9bd1be3 100644
--- a/warn/warn_bazel_api_test.go
+++ b/warn/warn_bazel_api_test.go
@@ -494,6 +494,58 @@ android_binary()
 		scopeBzl|scopeBuild)
 }
 
+func TestNativeCcWarning(t *testing.T) {
+	checkFindingsAndFix(t, "native-cc", `
+"""My file"""
+
+def macro():
+    cc_library()
+    native.cc_binary()
+    cc_test()
+    cc_proto_library()
+    native.fdo_prefetch_hints()
+    native.objc_library()
+    objc_import()
+    cc_toolchain()
+    native.cc_toolchain_suite()
+
+fdo_profile()
+cc_import()
+`, fmt.Sprintf(`
+"""My file"""
+
+load(%q, "cc_binary", "cc_import", "cc_library", "cc_proto_library", "cc_test", "cc_toolchain", "cc_toolchain_suite", "fdo_prefetch_hints", "fdo_profile", "objc_import", "objc_library")
+
+def macro():
+    cc_library()
+    cc_binary()
+    cc_test()
+    cc_proto_library()
+    fdo_prefetch_hints()
+    objc_library()
+    objc_import()
+    cc_toolchain()
+    cc_toolchain_suite()
+
+fdo_profile()
+cc_import()
+`, tables.CcLoadPath),
+		[]string{
+			fmt.Sprintf(`:4: Function "cc_library" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
+			fmt.Sprintf(`:5: Function "cc_binary" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
+			fmt.Sprintf(`:6: Function "cc_test" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
+			fmt.Sprintf(`:7: Function "cc_proto_library" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
+			fmt.Sprintf(`:8: Function "fdo_prefetch_hints" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
+			fmt.Sprintf(`:9: Function "objc_library" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
+			fmt.Sprintf(`:10: Function "objc_import" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
+			fmt.Sprintf(`:11: Function "cc_toolchain" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
+			fmt.Sprintf(`:12: Function "cc_toolchain_suite" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
+			fmt.Sprintf(`:14: Function "fdo_profile" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
+			fmt.Sprintf(`:15: Function "cc_import" is not global anymore and needs to be loaded from "%s".`, tables.CcLoadPath),
+		},
+		scopeBzl|scopeBuild)
+}
+
 func TestNativeJavaWarning(t *testing.T) {
 	checkFindingsAndFix(t, "native-java", `
 """My file"""