OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // Changes Blink-style names to Chrome-style names. Currently transforms: | 5 // Changes Blink-style names to Chrome-style names. Currently transforms: |
6 // fields: | 6 // fields: |
7 // int m_operationCount => int operation_count_ | 7 // int m_operationCount => int operation_count_ |
8 // variables (including parameters): | 8 // variables (including parameters): |
9 // int mySuperVariable => int my_super_variable | 9 // int mySuperVariable => int my_super_variable |
10 // constants: | 10 // constants: |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 // isEvaluable(). | 265 // isEvaluable(). |
266 if (initializer->isInstantiationDependent()) | 266 if (initializer->isInstantiationDependent()) |
267 return false; | 267 return false; |
268 | 268 |
269 // If the expression can be evaluated at compile time, then it should have a | 269 // If the expression can be evaluated at compile time, then it should have a |
270 // kFoo style name. Otherwise, not. | 270 // kFoo style name. Otherwise, not. |
271 return initializer->isEvaluatable(context); | 271 return initializer->isEvaluatable(context); |
272 } | 272 } |
273 | 273 |
274 bool GetNameForDecl(const clang::FunctionDecl& decl, | 274 bool GetNameForDecl(const clang::FunctionDecl& decl, |
275 const clang::ASTContext& context, | 275 clang::ASTContext& context, |
276 std::string& name) { | 276 std::string& name) { |
277 name = decl.getName().str(); | 277 name = decl.getName().str(); |
278 name[0] = clang::toUppercase(name[0]); | 278 name[0] = clang::toUppercase(name[0]); |
279 | 279 |
280 // https://crbug.com/582312: Prepend "Get" if method name conflicts with type. | 280 // https://crbug.com/582312: Prepend "Get" if method name conflicts with type. |
281 const clang::IdentifierInfo* return_type = | 281 const clang::IdentifierInfo* return_type = |
282 decl.getReturnType().getBaseTypeIdentifier(); | 282 decl.getReturnType().getBaseTypeIdentifier(); |
283 if (return_type && return_type->getName() == name) | 283 if (return_type && return_type->getName() == name) |
284 name = "Get" + name; | 284 name = "Get" + name; |
285 | 285 |
286 return true; | 286 return true; |
287 } | 287 } |
288 | 288 |
289 bool GetNameForDecl(const clang::EnumConstantDecl& decl, | 289 bool GetNameForDecl(const clang::EnumConstantDecl& decl, |
290 const clang::ASTContext& context, | 290 clang::ASTContext& context, |
291 std::string& name) { | 291 std::string& name) { |
292 StringRef original_name = decl.getName(); | 292 StringRef original_name = decl.getName(); |
293 | 293 |
294 // If it's already correct leave it alone. | 294 // If it's already correct leave it alone. |
295 if (original_name.size() >= 2 && original_name[0] == 'k' && | 295 if (original_name.size() >= 2 && original_name[0] == 'k' && |
296 clang::isUppercase(original_name[1])) | 296 clang::isUppercase(original_name[1])) |
297 return false; | 297 return false; |
298 | 298 |
299 bool is_shouty = true; | 299 bool is_shouty = true; |
300 for (char c : original_name) { | 300 for (char c : original_name) { |
301 if (!clang::isUppercase(c) && !clang::isDigit(c) && c != '_') { | 301 if (!clang::isUppercase(c) && !clang::isDigit(c) && c != '_') { |
302 is_shouty = false; | 302 is_shouty = false; |
303 break; | 303 break; |
304 } | 304 } |
305 } | 305 } |
306 | 306 |
307 if (is_shouty) | 307 if (is_shouty) |
308 return false; | 308 return false; |
309 | 309 |
310 name = 'k'; // k prefix on enum values. | 310 name = 'k'; // k prefix on enum values. |
311 name += original_name; | 311 name += original_name; |
312 name[1] = clang::toUppercase(name[1]); | 312 name[1] = clang::toUppercase(name[1]); |
313 return true; | 313 return true; |
314 } | 314 } |
315 | 315 |
316 bool GetNameForDecl(const clang::FieldDecl& decl, | 316 bool GetNameForDecl(const clang::FieldDecl& decl, |
317 const clang::ASTContext& context, | 317 clang::ASTContext& context, |
318 std::string& name) { | 318 std::string& name) { |
319 StringRef original_name = decl.getName(); | 319 StringRef original_name = decl.getName(); |
320 bool member_prefix = original_name.startswith(kBlinkFieldPrefix); | 320 bool member_prefix = original_name.startswith(kBlinkFieldPrefix); |
321 | 321 |
322 StringRef rename_part = !member_prefix | 322 StringRef rename_part = !member_prefix |
323 ? original_name | 323 ? original_name |
324 : original_name.substr(strlen(kBlinkFieldPrefix)); | 324 : original_name.substr(strlen(kBlinkFieldPrefix)); |
325 name = CamelCaseToUnderscoreCase(rename_part); | 325 name = CamelCaseToUnderscoreCase(rename_part); |
326 | 326 |
327 // Assume that prefix of m_ was intentional and always replace it with a | 327 // Assume that prefix of m_ was intentional and always replace it with a |
328 // suffix _. | 328 // suffix _. |
329 if (member_prefix && name.back() != '_') | 329 if (member_prefix && name.back() != '_') |
330 name += '_'; | 330 name += '_'; |
331 | 331 |
332 return true; | 332 return true; |
333 } | 333 } |
334 | 334 |
335 bool GetNameForDecl(const clang::VarDecl& decl, | 335 bool GetNameForDecl(const clang::VarDecl& decl, |
336 const clang::ASTContext& context, | 336 clang::ASTContext& context, |
337 std::string& name) { | 337 std::string& name) { |
338 StringRef original_name = decl.getName(); | 338 StringRef original_name = decl.getName(); |
339 | 339 |
340 // Nothing to do for unnamed parameters. | 340 // Nothing to do for unnamed parameters. |
341 if (clang::isa<clang::ParmVarDecl>(decl) && original_name.empty()) | 341 if (clang::isa<clang::ParmVarDecl>(decl)) { |
342 return false; | 342 if (original_name.empty()) |
| 343 return false; |
| 344 |
| 345 // Check if |decl| and |decl.getLocation| are in sync. We need to skip |
| 346 // out-of-sync ParmVarDecls to avoid renaming buggy ParmVarDecls that |
| 347 // 1) have decl.getLocation() pointing at a parameter declaration without a |
| 348 // name, but 2) have decl.getName() retained from a template specialization |
| 349 // of a method. See also: https://llvm.org/bugs/show_bug.cgi?id=29145 |
| 350 clang::SourceLocation loc = |
| 351 context.getSourceManager().getSpellingLoc(decl.getLocation()); |
| 352 auto parents = context.getParents(decl); |
| 353 bool is_child_location_within_parent_source_range = std::all_of( |
| 354 parents.begin(), parents.end(), |
| 355 [&loc](const clang::ast_type_traits::DynTypedNode& parent) { |
| 356 clang::SourceLocation begin = parent.getSourceRange().getBegin(); |
| 357 clang::SourceLocation end = parent.getSourceRange().getEnd(); |
| 358 return (begin < loc) && (loc < end); |
| 359 }); |
| 360 if (!is_child_location_within_parent_source_range) |
| 361 return false; |
| 362 } |
343 | 363 |
344 // static class members match against VarDecls. Blink style dictates that | 364 // static class members match against VarDecls. Blink style dictates that |
345 // these should be prefixed with `s_`, so strip that off. Also check for `m_` | 365 // these should be prefixed with `s_`, so strip that off. Also check for `m_` |
346 // and strip that off too, for code that accidentally uses the wrong prefix. | 366 // and strip that off too, for code that accidentally uses the wrong prefix. |
347 if (original_name.startswith(kBlinkStaticMemberPrefix)) | 367 if (original_name.startswith(kBlinkStaticMemberPrefix)) |
348 original_name = original_name.substr(strlen(kBlinkStaticMemberPrefix)); | 368 original_name = original_name.substr(strlen(kBlinkStaticMemberPrefix)); |
349 else if (original_name.startswith(kBlinkFieldPrefix)) | 369 else if (original_name.startswith(kBlinkFieldPrefix)) |
350 original_name = original_name.substr(strlen(kBlinkFieldPrefix)); | 370 original_name = original_name.substr(strlen(kBlinkFieldPrefix)); |
351 | 371 |
352 bool is_const = IsProbablyConst(decl, context); | 372 bool is_const = IsProbablyConst(decl, context); |
(...skipping 20 matching lines...) Expand all Loading... |
373 // Static members end with _ just like other members, but constants should | 393 // Static members end with _ just like other members, but constants should |
374 // not. | 394 // not. |
375 if (!is_const && decl.isStaticDataMember()) { | 395 if (!is_const && decl.isStaticDataMember()) { |
376 name += '_'; | 396 name += '_'; |
377 } | 397 } |
378 | 398 |
379 return true; | 399 return true; |
380 } | 400 } |
381 | 401 |
382 bool GetNameForDecl(const clang::FunctionTemplateDecl& decl, | 402 bool GetNameForDecl(const clang::FunctionTemplateDecl& decl, |
383 const clang::ASTContext& context, | 403 clang::ASTContext& context, |
384 std::string& name) { | 404 std::string& name) { |
385 clang::FunctionDecl* templated_function = decl.getTemplatedDecl(); | 405 clang::FunctionDecl* templated_function = decl.getTemplatedDecl(); |
386 return GetNameForDecl(*templated_function, context, name); | 406 return GetNameForDecl(*templated_function, context, name); |
387 } | 407 } |
388 | 408 |
389 bool GetNameForDecl(const clang::NamedDecl& decl, | 409 bool GetNameForDecl(const clang::NamedDecl& decl, |
390 const clang::ASTContext& context, | 410 clang::ASTContext& context, |
391 std::string& name) { | 411 std::string& name) { |
392 if (auto* function = clang::dyn_cast<clang::FunctionDecl>(&decl)) | 412 if (auto* function = clang::dyn_cast<clang::FunctionDecl>(&decl)) |
393 return GetNameForDecl(*function, context, name); | 413 return GetNameForDecl(*function, context, name); |
394 if (auto* var = clang::dyn_cast<clang::VarDecl>(&decl)) | 414 if (auto* var = clang::dyn_cast<clang::VarDecl>(&decl)) |
395 return GetNameForDecl(*var, context, name); | 415 return GetNameForDecl(*var, context, name); |
396 if (auto* field = clang::dyn_cast<clang::FieldDecl>(&decl)) | 416 if (auto* field = clang::dyn_cast<clang::FieldDecl>(&decl)) |
397 return GetNameForDecl(*field, context, name); | 417 return GetNameForDecl(*field, context, name); |
398 if (auto* function_template = | 418 if (auto* function_template = |
399 clang::dyn_cast<clang::FunctionTemplateDecl>(&decl)) | 419 clang::dyn_cast<clang::FunctionTemplateDecl>(&decl)) |
400 return GetNameForDecl(*function_template, context, name); | 420 return GetNameForDecl(*function_template, context, name); |
401 if (auto* enumc = clang::dyn_cast<clang::EnumConstantDecl>(&decl)) | 421 if (auto* enumc = clang::dyn_cast<clang::EnumConstantDecl>(&decl)) |
402 return GetNameForDecl(*enumc, context, name); | 422 return GetNameForDecl(*enumc, context, name); |
403 | 423 |
404 return false; | 424 return false; |
405 } | 425 } |
406 | 426 |
407 bool GetNameForDecl(const clang::UsingDecl& decl, | 427 bool GetNameForDecl(const clang::UsingDecl& decl, |
408 const clang::ASTContext& context, | 428 clang::ASTContext& context, |
409 std::string& name) { | 429 std::string& name) { |
410 assert(decl.shadow_size() > 0); | 430 assert(decl.shadow_size() > 0); |
411 | 431 |
412 // If a using declaration's targeted declaration is a set of overloaded | 432 // If a using declaration's targeted declaration is a set of overloaded |
413 // functions, it can introduce multiple shadowed declarations. Just using the | 433 // functions, it can introduce multiple shadowed declarations. Just using the |
414 // first one is OK, since overloaded functions have the same name, by | 434 // first one is OK, since overloaded functions have the same name, by |
415 // definition. | 435 // definition. |
416 return GetNameForDecl(*decl.shadow_begin()->getTargetDecl(), context, name); | 436 return GetNameForDecl(*decl.shadow_begin()->getTargetDecl(), context, name); |
417 } | 437 } |
418 | 438 |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
871 for (const auto& r : replacements) { | 891 for (const auto& r : replacements) { |
872 std::string replacement_text = r.getReplacementText().str(); | 892 std::string replacement_text = r.getReplacementText().str(); |
873 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 893 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
874 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 894 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
875 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 895 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
876 } | 896 } |
877 llvm::outs() << "==== END EDITS ====\n"; | 897 llvm::outs() << "==== END EDITS ====\n"; |
878 | 898 |
879 return 0; | 899 return 0; |
880 } | 900 } |
OLD | NEW |