blob: 68380ec700924b01105965083d39fed485aec04f [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 {
52 // Check if tidy is explicitly disabled for this module
53 if tidy.Properties.Tidy != nil && !*tidy.Properties.Tidy {
54 return flags
55 }
56
57 // If not explicitly set, check the global tidy flag
58 if tidy.Properties.Tidy == nil && !ctx.AConfig().ClangTidy() {
59 return flags
60 }
61
62 // Clang-tidy requires clang
63 if !flags.Clang {
64 return flags
65 }
66
67 flags.Tidy = true
68
69 CheckBadTidyFlags(ctx, "tidy_flags", tidy.Properties.Tidy_flags)
70
71 esc := proptools.NinjaAndShellEscape
72
73 flags.TidyFlags = append(flags.TidyFlags, esc(tidy.Properties.Tidy_flags)...)
74 if len(flags.TidyFlags) == 0 {
75 headerFilter := "-header-filter=\"(" + ctx.ModuleDir() + "|${config.TidyDefaultHeaderDirs})\""
76 flags.TidyFlags = append(flags.TidyFlags, headerFilter)
77 }
78
79 tidyChecks := "-checks="
80 if checks := ctx.AConfig().TidyChecks(); len(checks) > 0 {
81 tidyChecks += checks
82 } else {
83 tidyChecks += config.TidyChecksForDir(ctx.ModuleDir())
84 }
85 if len(tidy.Properties.Tidy_checks) > 0 {
86 CheckBadTidyChecks(ctx, "tidy_checks", tidy.Properties.Tidy_checks)
87
88 tidyChecks = tidyChecks + "," + strings.Join(esc(tidy.Properties.Tidy_checks), ",")
89 }
90 flags.TidyFlags = append(flags.TidyFlags, tidyChecks)
91
92 return flags
93}