1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <hidl-util/StringHelper.h>
18 #include <unordered_set>
19 #include <vector>
20 
21 #include "AST.h"
22 #include "CompoundType.h"
23 #include "EnumType.h"
24 #include "Lint.h"
25 #include "LintRegistry.h"
26 #include "Location.h"
27 #include "NamedType.h"
28 #include "Reference.h"
29 #include "Type.h"
30 
31 namespace android {
32 
namingConventions(const AST & ast,std::vector<Lint> * errors)33 static void namingConventions(const AST& ast, std::vector<Lint>* errors) {
34     std::unordered_set<const Type*> visited;
35     ast.getRootScope().recursivePass(
36             Type::ParseStage::COMPLETED,
37             [&](const Type* type) -> status_t {
38                 // Do not consider root scope
39                 if (type->parent() == nullptr) return OK;
40                 if (!type->isNamedType()) return OK;
41 
42                 const NamedType* namedType = static_cast<const NamedType*>(type);
43                 if (!Location::inSameFile(ast.getRootScope().location(), namedType->location())) {
44                     return OK;
45                 }
46 
47                 std::string definedName = namedType->definedName();
48                 // remove the "I" from the beginning
49                 if (namedType->isInterface()) definedName = definedName.substr(1);
50 
51                 std::string desiredName = StringHelper::ToPascalCase(definedName);
52 
53                 if (desiredName != definedName) {
54                     if (namedType->isInterface()) desiredName = "I" + desiredName;
55 
56                     errors->push_back(Lint(WARNING, namedType->location())
57                                       << "type \"" << namedType->definedName()
58                                       << "\" should be named \"" << desiredName
59                                       << "\" following the PascalCase (UpperCamelCase) "
60                                       << "naming convention.\n");
61                 }
62 
63                 if (namedType->isCompoundType()) {
64                     const CompoundType* compoundType = static_cast<const CompoundType*>(namedType);
65                     for (const NamedReference<Type>* ref : compoundType->getFields()) {
66                         std::string memberName = (*ref).name();
67                         if (StringHelper::ToCamelCase(memberName) != memberName) {
68                             errors->push_back(Lint(WARNING, (*ref).location())
69                                               << "member \"" << memberName << "\" of type \""
70                                               << compoundType->definedName()
71                                               << "\" should be named \""
72                                               << StringHelper::ToCamelCase(memberName)
73                                               << "\" following the camelCase naming convention.\n");
74                         }
75                     }
76                 } else if (namedType->isEnum()) {
77                     const EnumType* enumType = static_cast<const EnumType*>(namedType);
78                     for (const EnumValue* ev : enumType->values()) {
79                         if (StringHelper::ToUpperSnakeCase(ev->name()) != ev->name()) {
80                             errors->push_back(Lint(WARNING, ev->location())
81                                               << "enumeration \"" << ev->name() << "\" of enum \""
82                                               << enumType->definedName() << "\" should be named \""
83                                               << StringHelper::ToUpperSnakeCase(ev->name())
84                                               << "\" following the UPPER_SNAKE_CASE naming "
85                                               << "convention.\n");
86                         }
87                     }
88                 }
89 
90                 return OK;
91             },
92             &visited);
93 }
94 
95 REGISTER_LINT(namingConventions);
96 
97 }  // namespace android
98