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/strings.h>
18 #include <hidl-util/StringHelper.h>
19 #include <utils/Errors.h>
20 
21 #include <string>
22 #include <unordered_set>
23 #include <vector>
24 
25 #include "AST.h"
26 #include "EnumType.h"
27 #include "Lint.h"
28 #include "LintRegistry.h"
29 #include "Location.h"
30 #include "Type.h"
31 
32 namespace android {
enumValueNames(const AST & ast,std::vector<Lint> * errors)33 static void enumValueNames(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* t) -> status_t {
38                 if (!t->isEnum()) return OK;
39 
40                 const EnumType* enumType = static_cast<const EnumType*>(t);
41                 if (!Location::inSameFile(ast.getRootScope().location(), enumType->location())) {
42                     return OK;
43                 }
44                 for (const EnumValue* ev : enumType->values()) {
45                     const std::vector<std::string> tokens =
46                             base::Split(StringHelper::Uppercase(ev->name()), "_");
47 
48                     std::string errorString;
49                     for (const std::string& token : tokens) {
50                         if (token == "ALL" || token == "COUNT" || token == "MAX") {
51                             errorString = "\"" + token + "\"" +
52                                           " enum values have been known to become out of date when "
53                                           "people add minor version upgrades, extensions to "
54                                           "interfaces, or when more functionality is added later. "
55                                           "In order to make it easier to maintain interfaces, "
56                                           "consider avoiding adding this as part of an enum.\n";
57                             break;
58                         }
59                     }
60 
61                     if (!errorString.empty()) {
62                         errors->push_back(Lint(WARNING, ev->location(), errorString));
63                     }
64                 }
65 
66                 return OK;
67             },
68             &visited);
69 }
70 
71 REGISTER_LINT(enumValueNames);
72 };  // namespace android
73