Home | History | Annotate | Download | only in config
      1 // Copyright 2016 Google Inc. All rights reserved.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package config
     16 
     17 import (
     18 	"strings"
     19 
     20 	"android/soong/android"
     21 )
     22 
     23 func init() {
     24 	// Most Android source files are not clang-tidy clean yet.
     25 	// Global tidy checks include only google*, performance*,
     26 	// and misc-macro-parentheses, but not google-readability*
     27 	// or google-runtime-references.
     28 	pctx.VariableFunc("TidyDefaultGlobalChecks", func(config interface{}) (string, error) {
     29 		if override := config.(android.Config).Getenv("DEFAULT_GLOBAL_TIDY_CHECKS"); override != "" {
     30 			return override, nil
     31 		}
     32 		return strings.Join([]string{
     33 			"-*",
     34 			"google*",
     35 			"misc-macro-parentheses",
     36 			"performance*",
     37 			"-google-readability*",
     38 			"-google-runtime-references",
     39 		}, ","), nil
     40 	})
     41 
     42 	// There are too many clang-tidy warnings in external and vendor projects.
     43 	// Enable only some google checks for these projects.
     44 	pctx.VariableFunc("TidyExternalVendorChecks", func(config interface{}) (string, error) {
     45 		if override := config.(android.Config).Getenv("DEFAULT_EXTERNAL_VENDOR_TIDY_CHECKS"); override != "" {
     46 			return override, nil
     47 		}
     48 		return strings.Join([]string{
     49 			"-*",
     50 			"google*",
     51 			"-google-build-using-namespace",
     52 			"-google-default-arguments",
     53 			"-google-explicit-constructor",
     54 			"-google-readability*",
     55 			"-google-runtime-int",
     56 			"-google-runtime-references",
     57 		}, ","), nil
     58 	})
     59 
     60 	// Give warnings to header files only in selected directories.
     61 	// Do not give warnings to external or vendor header files, which contain too
     62 	// many warnings.
     63 	pctx.StaticVariable("TidyDefaultHeaderDirs", strings.Join([]string{
     64 		"art/",
     65 		"bionic/",
     66 		"bootable/",
     67 		"build/",
     68 		"cts/",
     69 		"dalvik/",
     70 		"developers/",
     71 		"development/",
     72 		"frameworks/",
     73 		"libcore/",
     74 		"libnativehelper/",
     75 		"system/",
     76 	}, "|"))
     77 }
     78 
     79 type PathBasedTidyCheck struct {
     80 	PathPrefix string
     81 	Checks     string
     82 }
     83 
     84 const tidyDefault = "${config.TidyDefaultGlobalChecks}"
     85 const tidyExternalVendor = "${config.TidyExternalVendorChecks}"
     86 
     87 // This is a map of local path prefixes to the set of default clang-tidy checks
     88 // to be used.
     89 // The last matched local_path_prefix should be the most specific to be used.
     90 var DefaultLocalTidyChecks = []PathBasedTidyCheck{
     91 	{"external/", tidyExternalVendor},
     92 	{"external/google", tidyDefault},
     93 	{"external/webrtc", tidyDefault},
     94 	{"frameworks/compile/mclinker/", tidyExternalVendor},
     95 	{"hardware/qcom", tidyExternalVendor},
     96 	{"vendor/", tidyExternalVendor},
     97 	{"vendor/google", tidyDefault},
     98 	{"vendor/google_devices", tidyExternalVendor},
     99 }
    100 
    101 var reversedDefaultLocalTidyChecks = reverseTidyChecks(DefaultLocalTidyChecks)
    102 
    103 func reverseTidyChecks(in []PathBasedTidyCheck) []PathBasedTidyCheck {
    104 	ret := make([]PathBasedTidyCheck, len(in))
    105 	for i, check := range in {
    106 		ret[len(in)-i-1] = check
    107 	}
    108 	return ret
    109 }
    110 
    111 func TidyChecksForDir(dir string) string {
    112 	for _, pathCheck := range reversedDefaultLocalTidyChecks {
    113 		if strings.HasPrefix(dir, pathCheck.PathPrefix) {
    114 			return pathCheck.Checks
    115 		}
    116 	}
    117 	return tidyDefault
    118 }
    119