Skip to content

Commit 0567de8

Browse files
committed
Comment AST: convert a huge if -- else if statement on Decl's type into a
switch. Thanks Sean Silva for suggestion! llvm-svn: 161225
1 parent 4c03dfd commit 0567de8

File tree

1 file changed

+56
-17
lines changed

1 file changed

+56
-17
lines changed

clang/lib/AST/Comment.cpp

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,22 @@ void DeclInfo::fill() {
151151
TemplateParameters = NULL;
152152

153153
if (!ThisDecl) {
154-
// Defaults are OK.
155-
} else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ThisDecl)) {
154+
// If there is no declaration, the defaults is our only guess.
155+
IsFilled = true;
156+
return;
157+
}
158+
159+
Decl::Kind K = ThisDecl->getKind();
160+
switch (K) {
161+
default:
162+
// Defaults are should be good for declarations we don't handle explicitly.
163+
break;
164+
case Decl::Function:
165+
case Decl::CXXMethod:
166+
case Decl::CXXConstructor:
167+
case Decl::CXXDestructor:
168+
case Decl::CXXConversion: {
169+
const FunctionDecl *FD = cast<FunctionDecl>(ThisDecl);
156170
Kind = FunctionKind;
157171
ParamVars = ArrayRef<const ParmVarDecl *>(FD->param_begin(),
158172
FD->getNumParams());
@@ -164,53 +178,78 @@ void DeclInfo::fill() {
164178
FD->getTemplateParameterList(NumLists - 1);
165179
}
166180

167-
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
181+
if (K == Decl::CXXMethod) {
182+
const CXXMethodDecl *MD = cast<CXXMethodDecl>(ThisDecl);
168183
IsInstanceMethod = MD->isInstance();
169184
IsClassMethod = !IsInstanceMethod;
170185
}
171-
} else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ThisDecl)) {
186+
break;
187+
}
188+
case Decl::ObjCMethod: {
189+
const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(ThisDecl);
172190
Kind = FunctionKind;
173191
ParamVars = ArrayRef<const ParmVarDecl *>(MD->param_begin(),
174192
MD->param_size());
175193
IsInstanceMethod = MD->isInstanceMethod();
176194
IsClassMethod = !IsInstanceMethod;
177-
} else if (const FunctionTemplateDecl *FTD =
178-
dyn_cast<FunctionTemplateDecl>(ThisDecl)) {
195+
break;
196+
}
197+
case Decl::FunctionTemplate: {
198+
const FunctionTemplateDecl *FTD = cast<FunctionTemplateDecl>(ThisDecl);
179199
Kind = FunctionKind;
180200
IsTemplateDecl = true;
181201
const FunctionDecl *FD = FTD->getTemplatedDecl();
182202
ParamVars = ArrayRef<const ParmVarDecl *>(FD->param_begin(),
183203
FD->getNumParams());
184204
TemplateParameters = FTD->getTemplateParameters();
185-
} else if (const ClassTemplateDecl *CTD =
186-
dyn_cast<ClassTemplateDecl>(ThisDecl)) {
205+
break;
206+
}
207+
case Decl::ClassTemplate: {
208+
const ClassTemplateDecl *CTD = cast<ClassTemplateDecl>(ThisDecl);
187209
Kind = ClassKind;
188210
IsTemplateDecl = true;
189211
TemplateParameters = CTD->getTemplateParameters();
190-
} else if (const ClassTemplatePartialSpecializationDecl *CTPSD =
191-
dyn_cast<ClassTemplatePartialSpecializationDecl>(ThisDecl)) {
212+
break;
213+
}
214+
case Decl::ClassTemplatePartialSpecialization: {
215+
const ClassTemplatePartialSpecializationDecl *CTPSD =
216+
cast<ClassTemplatePartialSpecializationDecl>(ThisDecl);
192217
Kind = ClassKind;
193218
IsTemplateDecl = true;
194219
IsTemplatePartialSpecialization = true;
195220
TemplateParameters = CTPSD->getTemplateParameters();
196-
} else if (isa<ClassTemplateSpecializationDecl>(ThisDecl)) {
221+
break;
222+
}
223+
case Decl::ClassTemplateSpecialization:
197224
Kind = ClassKind;
198225
IsTemplateDecl = true;
199226
IsTemplateSpecialization = true;
200-
} else if (isa<RecordDecl>(ThisDecl)) {
227+
break;
228+
case Decl::Record:
201229
Kind = ClassKind;
202-
} else if (isa<VarDecl>(ThisDecl) || isa<FieldDecl>(ThisDecl)) {
230+
break;
231+
case Decl::Var:
232+
case Decl::Field:
233+
case Decl::ObjCIvar:
234+
case Decl::ObjCAtDefsField:
203235
Kind = VariableKind;
204-
} else if (isa<NamespaceDecl>(ThisDecl)) {
236+
break;
237+
case Decl::Namespace:
205238
Kind = NamespaceKind;
206-
} else if (isa<TypedefNameDecl>(ThisDecl)) {
239+
break;
240+
case Decl::Typedef:
241+
case Decl::TypeAlias:
207242
Kind = TypedefKind;
208-
} else if (const TypeAliasTemplateDecl *TAT =
209-
dyn_cast<TypeAliasTemplateDecl>(ThisDecl)) {
243+
break;
244+
case Decl::TypeAliasTemplate: {
245+
const TypeAliasTemplateDecl *TAT = cast<TypeAliasTemplateDecl>(ThisDecl);
210246
Kind = TypedefKind;
211247
IsTemplateDecl = true;
212248
TemplateParameters = TAT->getTemplateParameters();
249+
break;
213250
}
251+
}
252+
214253
IsFilled = true;
215254
}
216255

0 commit comments

Comments
 (0)