@@ -305,27 +305,20 @@ std::string IdentifierNamingCheck::HungarianNotation::getDeclTypeName(
305
305
auto &SM = VD->getASTContext ().getSourceManager ();
306
306
const char *Begin = SM.getCharacterData (VD->getBeginLoc ());
307
307
const char *End = SM.getCharacterData (VD->getEndLoc ());
308
- intptr_t StrLen = End - Begin;
308
+ if (End < Begin)
309
+ return {};
309
310
310
311
// FIXME: Sometimes the value that returns from ValDecl->getEndLoc()
311
312
// is wrong(out of location of Decl). This causes `StrLen` will be assigned
312
313
// an unexpected large value. Current workaround to find the terminated
313
314
// character instead of the `getEndLoc()` function.
314
- const char *EOL = strchr (Begin, ' \n ' );
315
- if (!EOL)
316
- EOL = Begin + strlen (Begin);
317
-
318
- const char *PosList[] = {strchr (Begin, ' =' ), strchr (Begin, ' ;' ),
319
- strchr (Begin, ' ,' ), strchr (Begin, ' )' ), EOL};
320
- for (const auto &Pos : PosList) {
321
- if (Pos > Begin)
322
- EOL = std::min (EOL, Pos);
323
- }
315
+ llvm::StringRef NameRef (Begin, End - Begin);
316
+ auto Pos = NameRef.find_first_of (" \n\r =;,)" );
317
+ if (Pos != llvm::StringRef::npos)
318
+ NameRef = NameRef.substr (0 , Pos);
324
319
325
- StrLen = EOL - Begin;
326
- std::string TypeName;
327
- if (StrLen > 0 ) {
328
- std::string Type (Begin, StrLen);
320
+ if (!NameRef.empty ()) {
321
+ std::string Type (NameRef.begin (), NameRef.end ());
329
322
330
323
static constexpr StringRef Keywords[] = {
331
324
// Constexpr specifiers
@@ -339,66 +332,67 @@ std::string IdentifierNamingCheck::HungarianNotation::getDeclTypeName(
339
332
340
333
// Remove keywords
341
334
for (StringRef Kw : Keywords) {
342
- for (size_t Pos = 0 ;
343
- (Pos = Type.find (Kw.data (), Pos)) != std::string::npos;) {
335
+ for (size_t Pos = 0 ; (Pos = Type.find (Kw, Pos)) != std::string::npos;) {
344
336
Type.replace (Pos, Kw.size (), " " );
345
337
}
346
338
}
347
- TypeName = Type.erase (0 , Type.find_first_not_of (' ' ));
348
339
349
340
// Remove template parameters
350
341
const size_t Pos = Type.find (' <' );
351
342
if (Pos != std::string::npos) {
352
- TypeName = Type.erase (Pos, Type. size () - Pos);
343
+ Type.erase (Pos);
353
344
}
354
345
355
346
// Replace spaces with single space.
356
347
for (size_t Pos = 0 ; (Pos = Type.find (" " , Pos)) != std::string::npos;
357
- Pos += strlen ( " " ) ) {
358
- Type.replace (Pos, strlen ( " " ) , " " );
348
+ Pos += 2U ) {
349
+ Type.replace (Pos, 2U , " " );
359
350
}
360
351
361
352
// Replace " &" with "&".
362
353
for (size_t Pos = 0 ; (Pos = Type.find (" &" , Pos)) != std::string::npos;
363
- Pos += strlen ( " & " ) ) {
364
- Type.replace (Pos, strlen ( " & " ) , " &" );
354
+ Pos += 2U ) {
355
+ Type.replace (Pos, 2U , " &" );
365
356
}
366
357
367
358
// Replace " *" with "* ".
368
359
for (size_t Pos = 0 ; (Pos = Type.find (" *" , Pos)) != std::string::npos;
369
- Pos += strlen ( " * " ) ) {
370
- Type.replace (Pos, strlen ( " * " ) , " * " );
360
+ Pos += 1U ) {
361
+ Type.replace (Pos, 2U , " * " );
371
362
}
372
363
364
+ Type.erase (0 , Type.find_first_not_of (' ' ));
365
+
373
366
// Remove redundant tailing.
374
367
static constexpr StringRef TailsOfMultiWordType[] = {
375
368
" int" , " char" , " double" , " long" , " short" };
376
369
bool RedundantRemoved = false ;
377
370
for (auto Kw : TailsOfMultiWordType) {
378
- size_t Pos = Type.rfind (Kw. data () );
371
+ size_t Pos = Type.rfind (Kw);
379
372
if (Pos != std::string::npos) {
380
373
const size_t PtrCount = getAsteriskCount (Type, ND);
381
- Type = Type. substr ( 0 , Pos + Kw.size () + PtrCount);
374
+ Type. erase ( Pos + Kw.size () + PtrCount);
382
375
RedundantRemoved = true ;
383
376
break ;
384
377
}
385
378
}
386
379
387
- TypeName = Type.erase (0 , Type.find_first_not_of (' ' ));
380
+ Type.erase (0 , Type.find_first_not_of (' ' ));
388
381
if (!RedundantRemoved) {
389
382
std::size_t FoundSpace = Type.find (' ' );
390
383
if (FoundSpace != std::string::npos)
391
384
Type = Type.substr (0 , FoundSpace);
392
385
}
393
386
394
- TypeName = Type.erase (0 , Type.find_first_not_of (' ' ));
387
+ Type.erase (0 , Type.find_first_not_of (' ' ));
395
388
396
389
QualType QT = VD->getType ();
397
390
if (!QT.isNull () && QT->isArrayType ())
398
- TypeName.append (" []" );
391
+ Type.append (" []" );
392
+ return Type;
399
393
}
400
394
401
- return TypeName ;
395
+ return {} ;
402
396
}
403
397
404
398
IdentifierNamingCheck::IdentifierNamingCheck (StringRef Name,
@@ -1414,13 +1408,21 @@ IdentifierNamingCheck::getDiagInfo(const NamingCheckId &ID,
1414
1408
}};
1415
1409
}
1416
1410
1411
+ StringRef IdentifierNamingCheck::getRealFileName (StringRef FileName) const {
1412
+ auto Iter = RealFileNameCache.try_emplace (FileName);
1413
+ SmallString<256U > &RealFileName = Iter.first ->getValue ();
1414
+ if (!Iter.second )
1415
+ return RealFileName;
1416
+ llvm::sys::fs::real_path (FileName, RealFileName);
1417
+ return RealFileName;
1418
+ }
1419
+
1417
1420
const IdentifierNamingCheck::FileStyle &
1418
1421
IdentifierNamingCheck::getStyleForFile (StringRef FileName) const {
1419
1422
if (!GetConfigPerFile)
1420
1423
return *MainFileStyle;
1421
1424
1422
- SmallString<128 > RealFileName;
1423
- llvm::sys::fs::real_path (FileName, RealFileName);
1425
+ StringRef RealFileName = getRealFileName (FileName);
1424
1426
StringRef Parent = llvm::sys::path::parent_path (RealFileName);
1425
1427
auto Iter = NamingStylesCache.find (Parent);
1426
1428
if (Iter != NamingStylesCache.end ())
0 commit comments