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 <android-base/logging.h>
18 #include <android-base/parseint.h>
19 #include <hidl-util/FQName.h>
20 #include <hidl-util/StringHelper.h>
21 
22 #include <algorithm>
23 #include <string>
24 #include <vector>
25 
26 #include "AST.h"
27 #include "Interface.h"
28 #include "Lint.h"
29 #include "LintRegistry.h"
30 #include "Method.h"
31 
32 using namespace std::string_literals;
33 
34 namespace android {
getSanitizedMethodName(const Method & method)35 static std::string getSanitizedMethodName(const Method& method) {
36     size_t underscore = method.name().find('_');
37     return (underscore == std::string::npos) ? method.name() : method.name().substr(0, underscore);
38 }
39 
checkMethodVersion(const Method & method,const FQName & fqName,std::string * error)40 static bool checkMethodVersion(const Method& method, const FQName& fqName, std::string* error) {
41     size_t underscore = method.name().find('_');
42     CHECK(underscore != std::string::npos);  // only called on versionedMethods
43 
44     std::string version = method.name().substr(underscore + 1);  // don't include _
45     std::string nameWithoutVersion = method.name().substr(0, underscore);
46     underscore = version.find('_');
47 
48     std::string camelCaseMessage =
49             "Methods should follow the camelCase naming convention.\n"
50             "Underscores are only allowed in method names when defining a new version of a method. "
51             "Use the methodName_MAJOR_MINOR naming convention if that was the intended use. MAJOR, "
52             "MINOR must be integers representing the current package version.";
53 
54     if (nameWithoutVersion != StringHelper::ToCamelCase(nameWithoutVersion)) {
55         *error = camelCaseMessage;
56         return false;
57     }
58 
59     // does not contain both major and minor versions
60     if (underscore == std::string::npos) {
61         *error = camelCaseMessage;
62         return false;
63     }
64 
65     size_t major;
66     if (!base::ParseUint(version.substr(0, underscore), &major)) {
67         // could not parse a major rev
68         *error = camelCaseMessage;
69         return false;
70     }
71 
72     size_t minor;
73     if (!base::ParseUint(version.substr(underscore + 1), &minor)) {
74         // could not parse a minor rev
75         *error = camelCaseMessage;
76         return false;
77     }
78 
79     if ((major == fqName.getPackageMajorVersion()) && (minor == fqName.getPackageMinorVersion())) {
80         return true;
81     }
82 
83     *error = method.name() + " looks like version "s + std::to_string(major) + "."s +
84              std::to_string(minor) + " of "s + getSanitizedMethodName(method) +
85              ", but the interface is in package version "s + fqName.version();
86     return false;
87 }
88 
methodVersions(const AST & ast,std::vector<Lint> * errors)89 static void methodVersions(const AST& ast, std::vector<Lint>* errors) {
90     const Interface* iface = ast.getInterface();
91     if (iface == nullptr) {
92         // No interfaces so no methods.
93         return;
94     }
95 
96     for (Method* method : iface->userDefinedMethods()) {
97         if (method->name().find('_') == std::string::npos) {
98             if (method->name() != StringHelper::ToCamelCase(method->name())) {
99                 errors->push_back(Lint(WARNING, method->location(),
100                                        "Methods should follow the camelCase naming convention.\n"));
101             }
102 
103             continue;
104         }
105 
106         // the method has been versioned
107         std::string errorString;
108         if (checkMethodVersion(*method, ast.package(), &errorString)) {
109             // Ensure that a supertype contains the method
110             std::string methodName = getSanitizedMethodName(*method);
111 
112             const std::vector<const Interface*>& superTypeChain = iface->superTypeChain();
113             bool foundMethod = std::any_of(
114                     superTypeChain.begin(), superTypeChain.end(),
115                     [&](const Interface* superType) -> bool {
116                         const std::vector<Method*>& superMethods = superType->userDefinedMethods();
117                         return std::any_of(superMethods.begin(), superMethods.end(),
118                                            [&](Method* superMethod) -> bool {
119                                                return getSanitizedMethodName(*superMethod) ==
120                                                       methodName;
121                                            });
122                     });
123 
124             if (!foundMethod) {
125                 errors->push_back(Lint(WARNING, method->location())
126                                   << "Could not find method " << methodName
127                                   << " in any of the super types.\n"
128                                   << "Should only use the method_X_Y naming convention when the "
129                                   << "method is replacing an older version of the same method.\n");
130             }
131         } else {
132             errors->push_back(Lint(WARNING, method->location()) << errorString << "\n");
133         }
134     }
135 }
136 
137 REGISTER_LINT(methodVersions);
138 }  // namespace android
139