-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[NFC][Clang] Use StringRef and range for loops in SA/Syntax Emitters #115972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jurahul
merged 1 commit into
llvm:main
from
jurahul:clang_sa_syntax_emitter_strref_range_loops
Nov 13, 2024
Merged
[NFC][Clang] Use StringRef and range for loops in SA/Syntax Emitters #115972
jurahul
merged 1 commit into
llvm:main
from
jurahul:clang_sa_syntax_emitter_strref_range_loops
Nov 13, 2024
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Use StringRef and range for loops in Clang SACheckers and Syntax emitters.
@llvm/pr-subscribers-clang Author: Rahul Joshi (jurahul) ChangesUse StringRef and range for loops in Clang SACheckers and Syntax emitters. Full diff: https://github.com/llvm/llvm-project/pull/115972.diff 2 Files Affected:
diff --git a/clang/utils/TableGen/ClangSACheckersEmitter.cpp b/clang/utils/TableGen/ClangSACheckersEmitter.cpp
index 36012dbf70791b..097cbf3edac041 100644
--- a/clang/utils/TableGen/ClangSACheckersEmitter.cpp
+++ b/clang/utils/TableGen/ClangSACheckersEmitter.cpp
@@ -28,10 +28,9 @@ static std::string getPackageFullName(const Record *R, StringRef Sep = ".");
static std::string getParentPackageFullName(const Record *R,
StringRef Sep = ".") {
- std::string name;
if (const DefInit *DI = dyn_cast<DefInit>(R->getValueInit("ParentPackage")))
- name = getPackageFullName(DI->getDef(), Sep);
- return name;
+ return getPackageFullName(DI->getDef(), Sep);
+ return "";
}
static std::string getPackageFullName(const Record *R, StringRef Sep) {
@@ -52,10 +51,10 @@ static std::string getCheckerFullName(const Record *R, StringRef Sep = ".") {
return name;
}
-static std::string getStringValue(const Record &R, StringRef field) {
+static StringRef getStringValue(const Record &R, StringRef field) {
if (const StringInit *SI = dyn_cast<StringInit>(R.getValueInit(field)))
- return std::string(SI->getValue());
- return std::string();
+ return SI->getValue();
+ return "";
}
// Calculates the integer value representing the BitsInit object
@@ -93,7 +92,7 @@ static std::string getCheckerDocs(const Record &R) {
/// Retrieves the type from a CmdOptionTypeEnum typed Record object. Note that
/// the class itself has to be modified for adding a new option type in
/// CheckerBase.td.
-static std::string getCheckerOptionType(const Record &R) {
+static StringRef getCheckerOptionType(const Record &R) {
if (const BitsInit *BI = R.getValueAsBitsInit("Type")) {
switch(getValueFromBitsInit(BI, R)) {
case 0:
@@ -110,7 +109,7 @@ static std::string getCheckerOptionType(const Record &R) {
return "";
}
-static std::string getDevelopmentStage(const Record &R) {
+static StringRef getDevelopmentStage(const Record &R) {
if (const BitsInit *BI = R.getValueAsBitsInit("DevelopmentStage")) {
switch(getValueFromBitsInit(BI, R)) {
case 0:
@@ -179,8 +178,6 @@ void clang::EmitClangSACheckers(const RecordKeeper &Records, raw_ostream &OS) {
ArrayRef<const Record *> packages =
Records.getAllDerivedDefinitions("Package");
- using SortedRecords = StringMap<const Record *>;
-
OS << "// This file is automatically generated. Do not edit this file by "
"hand.\n";
@@ -191,16 +188,13 @@ void clang::EmitClangSACheckers(const RecordKeeper &Records, raw_ostream &OS) {
OS << "\n"
"#ifdef GET_PACKAGES\n";
{
- SortedRecords sortedPackages;
- for (unsigned i = 0, e = packages.size(); i != e; ++i)
- sortedPackages[getPackageFullName(packages[i])] = packages[i];
-
- for (SortedRecords::iterator
- I = sortedPackages.begin(), E = sortedPackages.end(); I != E; ++I) {
- const Record &R = *I->second;
-
+ StringMap<const Record *> sortedPackages;
+ for (const Record *Package : packages)
+ sortedPackages[getPackageFullName(Package)] = Package;
+
+ for (const auto &[_, R] : sortedPackages) {
OS << "PACKAGE(" << "\"";
- OS.write_escaped(getPackageFullName(&R)) << '\"';
+ OS.write_escaped(getPackageFullName(R)) << '\"';
OS << ")\n";
}
}
@@ -225,7 +219,6 @@ void clang::EmitClangSACheckers(const RecordKeeper &Records, raw_ostream &OS) {
OS << "\n"
"#ifdef GET_PACKAGE_OPTIONS\n";
for (const Record *Package : packages) {
-
if (Package->isValueUnset("PackageOptions"))
continue;
@@ -250,9 +243,9 @@ void clang::EmitClangSACheckers(const RecordKeeper &Records, raw_ostream &OS) {
OS << "\n"
"#ifdef GET_CHECKERS\n"
"\n";
- for (const Record *checker : checkers) {
+ for (const Record *checker : checkers)
printChecker(OS, *checker);
- }
+
OS << "\n"
"#endif // GET_CHECKERS\n"
"\n";
diff --git a/clang/utils/TableGen/ClangSyntaxEmitter.cpp b/clang/utils/TableGen/ClangSyntaxEmitter.cpp
index 4098a5e88e6820..6800ad300acd3c 100644
--- a/clang/utils/TableGen/ClangSyntaxEmitter.cpp
+++ b/clang/utils/TableGen/ClangSyntaxEmitter.cpp
@@ -116,13 +116,13 @@ struct SyntaxConstraint {
} else if (R.isSubClassOf("AnyToken")) {
NodeType = "Leaf";
} else if (R.isSubClassOf("NodeType")) {
- NodeType = R.getName().str();
+ NodeType = R.getName();
} else {
assert(false && "Unhandled Syntax kind");
}
}
- std::string NodeType;
+ StringRef NodeType;
// optional and leaf types also go here, once we want to use them.
};
|
kazutakahirata
approved these changes
Nov 13, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Use StringRef and range for loops in Clang SACheckers and Syntax emitters.