blob: 2216f587387656a7e7b7583b29b27709c54f4f65 [file] [log] [blame]
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001// 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
15package cc
16
17import (
18 "strings"
19
20 "github.com/google/blueprint/proptools"
21
22 "android/soong/cc/config"
23)
24
25type TidyProperties struct {
26 // whether to run clang-tidy over C-like sources.
27 Tidy *bool
28
29 // Extra flags to pass to clang-tidy
30 Tidy_flags []string
31
32 // Extra checks to enable or disable in clang-tidy
33 Tidy_checks []string
34}
35
36type tidyFeature struct {
37 Properties TidyProperties
38}
39
40func (tidy *tidyFeature) props() []interface{} {
41 return []interface{}{&tidy.Properties}
42}
43
44func (tidy *tidyFeature) begin(ctx BaseModuleContext) {
45}
46
47func (tidy *tidyFeature) deps(ctx BaseModuleContext, deps Deps) Deps {
48 return deps
49}
50
51func (tidy *tidyFeature) flags(ctx ModuleContext, flags Flags) Flags {
Colin Cross379d2cb2016-12-05 17:11:06 -080052 CheckBadTidyFlags(ctx, "tidy_flags", tidy.Properties.Tidy_flags)
53 CheckBadTidyChecks(ctx, "tidy_checks", tidy.Properties.Tidy_checks)
54
Dan Willemsena03cf6d2016-09-26 15:45:04 -070055 // Check if tidy is explicitly disabled for this module
56 if tidy.Properties.Tidy != nil && !*tidy.Properties.Tidy {
57 return flags
58 }
59
60 // If not explicitly set, check the global tidy flag
61 if tidy.Properties.Tidy == nil && !ctx.AConfig().ClangTidy() {
62 return flags
63 }
64
65 // Clang-tidy requires clang
66 if !flags.Clang {
67 return flags
68 }
69
70 flags.Tidy = true
71
Dan Willemsena03cf6d2016-09-26 15:45:04 -070072 esc := proptools.NinjaAndShellEscape
73
74 flags.TidyFlags = append(flags.TidyFlags, esc(tidy.Properties.Tidy_flags)...)
75 if len(flags.TidyFlags) == 0 {
76 headerFilter := "-header-filter=\"(" + ctx.ModuleDir() + "|${config.TidyDefaultHeaderDirs})\""
77 flags.TidyFlags = append(flags.TidyFlags, headerFilter)
78 }
79
80 tidyChecks := "-checks="
81 if checks := ctx.AConfig().TidyChecks(); len(checks) > 0 {
82 tidyChecks += checks
83 } else {
84 tidyChecks += config.TidyChecksForDir(ctx.ModuleDir())
85 }
86 if len(tidy.Properties.Tidy_checks) > 0 {
Dan Willemsena03cf6d2016-09-26 15:45:04 -070087 tidyChecks = tidyChecks + "," + strings.Join(esc(tidy.Properties.Tidy_checks), ",")
88 }
89 flags.TidyFlags = append(flags.TidyFlags, tidyChecks)
90
91 return flags
92}