Skip to content

Commit c785eae

Browse files
authored
[RISCV] Add wrapper function for getStringError in RISCVISAInfo. NFC (#97478)
We use the same error code for all errors. Add a wrapper so we don't have to repeat it.
1 parent 0357945 commit c785eae

File tree

1 file changed

+65
-101
lines changed

1 file changed

+65
-101
lines changed

llvm/lib/TargetParser/RISCVISAInfo.cpp

Lines changed: 65 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -282,15 +282,17 @@ std::vector<std::string> RISCVISAInfo::toFeatures(bool AddAllExtensions,
282282
return Features;
283283
}
284284

285-
static Error getStringErrorForInvalidExt(StringRef ExtName) {
285+
static Error getError(const Twine &Message) {
286+
return createStringError(errc::invalid_argument, Message);
287+
}
288+
289+
static Error getErrorForInvalidExt(StringRef ExtName) {
286290
if (ExtName.size() == 1) {
287-
return createStringError(errc::invalid_argument,
288-
"unsupported standard user-level extension '" +
289-
ExtName + "'");
291+
return getError("unsupported standard user-level extension '" + ExtName +
292+
"'");
290293
}
291-
return createStringError(errc::invalid_argument,
292-
"unsupported " + getExtensionTypeDesc(ExtName) +
293-
" '" + ExtName + "'");
294+
return getError("unsupported " + getExtensionTypeDesc(ExtName) + " '" +
295+
ExtName + "'");
294296
}
295297

296298
// Extensions may have a version number, and may be separated by
@@ -315,21 +317,18 @@ static Error getExtensionVersion(StringRef Ext, StringRef In, unsigned &Major,
315317

316318
// Expected 'p' to be followed by minor version number.
317319
if (MinorStr.empty()) {
318-
return createStringError(
319-
errc::invalid_argument,
320-
"minor version number missing after 'p' for extension '" + Ext + "'");
320+
return getError("minor version number missing after 'p' for extension '" +
321+
Ext + "'");
321322
}
322323
}
323324

324325
if (!MajorStr.empty() && MajorStr.getAsInteger(10, Major))
325-
return createStringError(
326-
errc::invalid_argument,
327-
"Failed to parse major version number for extension '" + Ext + "'");
326+
return getError("Failed to parse major version number for extension '" +
327+
Ext + "'");
328328

329329
if (!MinorStr.empty() && MinorStr.getAsInteger(10, Minor))
330-
return createStringError(
331-
errc::invalid_argument,
332-
"Failed to parse minor version number for extension '" + Ext + "'");
330+
return getError("Failed to parse minor version number for extension '" +
331+
Ext + "'");
333332

334333
ConsumeLength = MajorStr.size();
335334

@@ -340,24 +339,21 @@ static Error getExtensionVersion(StringRef Ext, StringRef In, unsigned &Major,
340339
// subsequent characters (i.e. must either end string or be followed by
341340
// an underscore).
342341
if (Ext.size() > 1 && In.size())
343-
return createStringError(
344-
errc::invalid_argument,
342+
return getError(
345343
"multi-character extensions must be separated by underscores");
346344

347345
// If experimental extension, require use of current version number
348346
if (auto ExperimentalExtension = isExperimentalExtension(Ext)) {
349347
if (!EnableExperimentalExtension)
350-
return createStringError(errc::invalid_argument,
351-
"requires '-menable-experimental-extensions' "
352-
"for experimental extension '" +
353-
Ext + "'");
348+
return getError("requires '-menable-experimental-extensions' "
349+
"for experimental extension '" +
350+
Ext + "'");
354351

355352
if (ExperimentalExtensionVersionCheck &&
356353
(MajorStr.empty() && MinorStr.empty()))
357-
return createStringError(
358-
errc::invalid_argument,
354+
return getError(
359355
"experimental extension requires explicit version number `" + Ext +
360-
"`");
356+
"`");
361357

362358
auto SupportedVers = *ExperimentalExtension;
363359
if (ExperimentalExtensionVersionCheck &&
@@ -368,7 +364,7 @@ static Error getExtensionVersion(StringRef Ext, StringRef In, unsigned &Major,
368364
Error += " for experimental extension '" + Ext.str() +
369365
"' (this compiler supports " + utostr(SupportedVers.Major) +
370366
"." + utostr(SupportedVers.Minor) + ")";
371-
return createStringError(errc::invalid_argument, Error);
367+
return getError(Error);
372368
}
373369
return Error::success();
374370
}
@@ -392,13 +388,13 @@ static Error getExtensionVersion(StringRef Ext, StringRef In, unsigned &Major,
392388
return Error::success();
393389

394390
if (!RISCVISAInfo::isSupportedExtension(Ext))
395-
return getStringErrorForInvalidExt(Ext);
391+
return getErrorForInvalidExt(Ext);
396392

397-
std::string Error = "unsupported version number " + std::string(MajorStr);
393+
std::string Error = "unsupported version number " + MajorStr.str();
398394
if (!MinorStr.empty())
399395
Error += "." + MinorStr.str();
400396
Error += " for extension '" + Ext.str() + "'";
401-
return createStringError(errc::invalid_argument, Error);
397+
return getError(Error);
402398
}
403399

404400
llvm::Expected<std::unique_ptr<RISCVISAInfo>>
@@ -439,8 +435,7 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
439435
// RISC-V ISA strings must be [a-z0-9_]
440436
if (!llvm::all_of(
441437
Arch, [](char C) { return isDigit(C) || isLower(C) || C == '_'; }))
442-
return createStringError(errc::invalid_argument,
443-
"string may only contain [a-z0-9_]");
438+
return getError("string may only contain [a-z0-9_]");
444439

445440
// Must start with a valid base ISA name.
446441
unsigned XLen = 0;
@@ -450,8 +445,7 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
450445
XLen = 64;
451446

452447
if (XLen == 0 || Arch.empty() || (Arch[0] != 'i' && Arch[0] != 'e'))
453-
return createStringError(errc::invalid_argument,
454-
"arch string must begin with valid base ISA");
448+
return getError("arch string must begin with valid base ISA");
455449

456450
std::unique_ptr<RISCVISAInfo> ISAInfo(new RISCVISAInfo(XLen));
457451

@@ -461,8 +455,7 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
461455
while (!Arch.empty()) {
462456
if (Arch[0] == '_') {
463457
if (Arch.size() == 1 || Arch[1] == '_')
464-
return createStringError(errc::invalid_argument,
465-
"extension name missing after separator '_'");
458+
return getError("extension name missing after separator '_'");
466459
Arch = Arch.drop_front();
467460
}
468461

@@ -473,12 +466,10 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
473466
StringRef Prefix, MinorVersionStr;
474467
std::tie(Prefix, MinorVersionStr) = Ext.rsplit('p');
475468
if (MinorVersionStr.empty())
476-
return createStringError(errc::invalid_argument,
477-
"extension lacks version in expected format");
469+
return getError("extension lacks version in expected format");
478470
unsigned MajorVersion, MinorVersion;
479471
if (MinorVersionStr.getAsInteger(10, MinorVersion))
480-
return createStringError(errc::invalid_argument,
481-
"failed to parse minor version number");
472+
return getError("failed to parse minor version number");
482473

483474
// Split Prefix into the extension name and the major version number
484475
// (the trailing digits of Prefix).
@@ -489,32 +480,27 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
489480
--VersionStart;
490481
}
491482
if (VersionStart == Prefix.size())
492-
return createStringError(errc::invalid_argument,
493-
"extension lacks version in expected format");
483+
return getError("extension lacks version in expected format");
494484

495485
if (VersionStart == 0)
496-
return createStringError(errc::invalid_argument,
497-
"missing extension name");
486+
return getError("missing extension name");
498487

499488
StringRef ExtName = Prefix.slice(0, VersionStart);
500489
StringRef MajorVersionStr = Prefix.slice(VersionStart, StringRef::npos);
501490
if (MajorVersionStr.getAsInteger(10, MajorVersion))
502-
return createStringError(errc::invalid_argument,
503-
"failed to parse major version number");
491+
return getError("failed to parse major version number");
504492

505493
if ((ExtName[0] == 'z' || ExtName[0] == 's' || ExtName[0] == 'x') &&
506494
(ExtName.size() == 1 || isDigit(ExtName[1])))
507-
return createStringError(errc::invalid_argument,
508-
"'" + Twine(ExtName[0]) +
509-
"' must be followed by a letter");
495+
return getError("'" + Twine(ExtName[0]) +
496+
"' must be followed by a letter");
510497

511498
if (!ISAInfo->Exts
512499
.emplace(
513500
ExtName.str(),
514501
RISCVISAUtils::ExtensionVersion{MajorVersion, MinorVersion})
515502
.second)
516-
return createStringError(errc::invalid_argument,
517-
"duplicate extension '" + ExtName + "'");
503+
return getError("duplicate extension '" + ExtName + "'");
518504
}
519505
ISAInfo->updateImpliedLengths();
520506
return std::move(ISAInfo);
@@ -526,8 +512,7 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
526512
// RISC-V ISA strings must be [a-z0-9_]
527513
if (!llvm::all_of(
528514
Arch, [](char C) { return isDigit(C) || isLower(C) || C == '_'; }))
529-
return createStringError(errc::invalid_argument,
530-
"string may only contain [a-z0-9_]");
515+
return getError("string may only contain [a-z0-9_]");
531516

532517
// ISA string must begin with rv32, rv64, or a profile.
533518
unsigned XLen = 0;
@@ -548,10 +533,9 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
548533
FoundProfile = (I != std::begin(SupportedExperimentalProfiles) &&
549534
Arch.starts_with(std::prev(I)->Name));
550535
if (FoundProfile && !EnableExperimentalExtension) {
551-
return createStringError(errc::invalid_argument,
552-
"requires '-menable-experimental-extensions' "
553-
"for profile '" +
554-
std::prev(I)->Name + "'");
536+
return getError("requires '-menable-experimental-extensions' "
537+
"for profile '" +
538+
std::prev(I)->Name + "'");
555539
}
556540
}
557541
if (FoundProfile) {
@@ -560,9 +544,7 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
560544
StringRef ArchWithoutProfile = Arch.drop_front(I->Name.size());
561545
if (!ArchWithoutProfile.empty()) {
562546
if (ArchWithoutProfile.front() != '_')
563-
return createStringError(
564-
errc::invalid_argument,
565-
"additional extensions must be after separator '_'");
547+
return getError("additional extensions must be after separator '_'");
566548
NewArch += ArchWithoutProfile.str();
567549
}
568550
return parseArchString(NewArch, EnableExperimentalExtension,
@@ -571,8 +553,7 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
571553
}
572554

573555
if (XLen == 0 || Arch.empty())
574-
return createStringError(
575-
errc::invalid_argument,
556+
return getError(
576557
"string must begin with rv32{i,e,g}, rv64{i,e,g}, or a supported "
577558
"profile name");
578559

@@ -592,9 +573,8 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
592573
// First letter should be 'e', 'i' or 'g'.
593574
switch (Baseline) {
594575
default:
595-
return createStringError(errc::invalid_argument,
596-
"first letter after \'rv" + Twine(XLen) +
597-
"\' should be 'e', 'i' or 'g'");
576+
return getError("first letter after \'rv" + Twine(XLen) +
577+
"\' should be 'e', 'i' or 'g'");
598578
case 'e':
599579
case 'i':
600580
// Baseline is `i` or `e`
@@ -609,8 +589,7 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
609589
case 'g':
610590
// g expands to extensions in RISCVGImplications.
611591
if (Arch.size() > 1 && isDigit(Arch[1]))
612-
return createStringError(errc::invalid_argument,
613-
"version not supported for 'g'");
592+
return getError("version not supported for 'g'");
614593

615594
// Versions for g are disallowed, and this was checked for previously.
616595
ConsumeLength = 0;
@@ -634,8 +613,7 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
634613
while (!Exts.empty()) {
635614
if (Exts.front() == '_') {
636615
if (Exts.size() == 1 || Exts[1] == '_')
637-
return createStringError(errc::invalid_argument,
638-
"extension name missing after separator '_'");
616+
return getError("extension name missing after separator '_'");
639617
Exts = Exts.drop_front();
640618
}
641619

@@ -669,12 +647,10 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
669647

670648
assert(!Type.empty() && "Empty type?");
671649
if (Name.size() == Type.size())
672-
return createStringError(errc::invalid_argument,
673-
Desc + " name missing after '" + Type + "'");
650+
return getError(Desc + " name missing after '" + Type + "'");
674651
} else {
675-
return createStringError(errc::invalid_argument,
676-
"invalid standard user-level extension '" +
677-
Twine(Ext.front()) + "'");
652+
return getError("invalid standard user-level extension '" +
653+
Twine(Ext.front()) + "'");
678654
}
679655

680656
unsigned Major, Minor, ConsumeLength;
@@ -688,8 +664,7 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
688664

689665
// Check if duplicated extension.
690666
if (SeenExtMap.contains(Name.str()))
691-
return createStringError(errc::invalid_argument,
692-
"duplicated " + Desc + " '" + Name + "'");
667+
return getError("duplicated " + Desc + " '" + Name + "'");
693668

694669
SeenExtMap[Name.str()] = {Major, Minor};
695670
} while (!Ext.empty());
@@ -700,7 +675,7 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
700675
const std::string &ExtName = SeenExtAndVers.first;
701676

702677
if (!RISCVISAInfo::isSupportedExtension(ExtName))
703-
return getStringErrorForInvalidExt(ExtName);
678+
return getErrorForInvalidExt(ExtName);
704679
ISAInfo->Exts[ExtName] = SeenExtAndVers.second;
705680
}
706681

@@ -718,60 +693,49 @@ Error RISCVISAInfo::checkDependency() {
718693
bool HasZcmt = Exts.count("zcmt") != 0;
719694

720695
if (HasI && HasE)
721-
return createStringError(errc::invalid_argument,
722-
"'I' and 'E' extensions are incompatible");
696+
return getError("'I' and 'E' extensions are incompatible");
723697

724698
if (HasF && HasZfinx)
725-
return createStringError(errc::invalid_argument,
726-
"'f' and 'zfinx' extensions are incompatible");
699+
return getError("'f' and 'zfinx' extensions are incompatible");
727700

728701
if (HasZvl && !HasVector)
729-
return createStringError(
730-
errc::invalid_argument,
702+
return getError(
731703
"'zvl*b' requires 'v' or 'zve*' extension to also be specified");
732704

733705
if (Exts.count("zvbb") && !HasVector)
734-
return createStringError(
735-
errc::invalid_argument,
706+
return getError(
736707
"'zvbb' requires 'v' or 'zve*' extension to also be specified");
737708

738709
if (Exts.count("zvbc") && !Exts.count("zve64x"))
739-
return createStringError(
740-
errc::invalid_argument,
710+
return getError(
741711
"'zvbc' requires 'v' or 'zve64*' extension to also be specified");
742712

743713
if ((Exts.count("zvkb") || Exts.count("zvkg") || Exts.count("zvkned") ||
744714
Exts.count("zvknha") || Exts.count("zvksed") || Exts.count("zvksh")) &&
745715
!HasVector)
746-
return createStringError(
747-
errc::invalid_argument,
716+
return getError(
748717
"'zvk*' requires 'v' or 'zve*' extension to also be specified");
749718

750719
if (Exts.count("zvknhb") && !Exts.count("zve64x"))
751-
return createStringError(
752-
errc::invalid_argument,
720+
return getError(
753721
"'zvknhb' requires 'v' or 'zve64*' extension to also be specified");
754722

755723
if ((HasZcmt || Exts.count("zcmp")) && Exts.count("d") &&
756724
(HasC || Exts.count("zcd")))
757-
return createStringError(
758-
errc::invalid_argument,
759-
Twine("'") + (HasZcmt ? "zcmt" : "zcmp") +
760-
"' extension is incompatible with '" + (HasC ? "c" : "zcd") +
761-
"' extension when 'd' extension is enabled");
725+
return getError(Twine("'") + (HasZcmt ? "zcmt" : "zcmp") +
726+
"' extension is incompatible with '" +
727+
(HasC ? "c" : "zcd") +
728+
"' extension when 'd' extension is enabled");
762729

763730
if (XLen != 32 && Exts.count("zcf"))
764-
return createStringError(errc::invalid_argument,
765-
"'zcf' is only supported for 'rv32'");
731+
return getError("'zcf' is only supported for 'rv32'");
766732

767733
if (Exts.count("zacas") && !(Exts.count("a") || Exts.count("zaamo")))
768-
return createStringError(
769-
errc::invalid_argument,
734+
return getError(
770735
"'zacas' requires 'a' or 'zaamo' extension to also be specified");
771736

772737
if (Exts.count("zabha") && !(Exts.count("a") || Exts.count("zaamo")))
773-
return createStringError(
774-
errc::invalid_argument,
738+
return getError(
775739
"'zabha' requires 'a' or 'zaamo' extension to also be specified");
776740

777741
return Error::success();

0 commit comments

Comments
 (0)