[flang-commits] [flang] [flang][OpenMP] Issue a warning when parsing future directive spelling (PR #147765)
Jack Styles via flang-commits
flang-commits at lists.llvm.org
Thu Jul 10 07:46:39 PDT 2025
================
@@ -259,6 +259,57 @@ void OmpStructureChecker::CheckVariableListItem(
}
}
+void OmpStructureChecker::CheckDirectiveSpelling(
+ parser::CharBlock spelling, llvm::omp::Directive id) {
+ // Directive names that contain spaces can be spelled in the source without
+ // any of the spaces. Because of that getOpenMPKind* is not guaranteed to
+ // work with the source spelling as the argument.
+ //
+ // To verify the source spellings, we have to get the spelling for a given
+ // version, remove spaces and compare it with the source spelling (also
+ // with spaces removed).
+ auto removeSpaces = [](llvm::StringRef s) {
+ std::string n{s.str()};
+ for (size_t idx{n.size()}; idx > 0; --idx) {
+ if (isspace(n[idx - 1])) {
+ n.erase(idx - 1, 1);
+ }
+ }
+ return n;
+ };
+
+ std::string lowerNoWS{removeSpaces(
+ parser::ToLowerCaseLetters({spelling.begin(), spelling.size()}))};
+ llvm::StringRef ref(lowerNoWS);
+ if (ref.starts_with("end")) {
+ ref = ref.drop_front(3);
+ }
+
+ unsigned version{context_.langOptions().OpenMPVersion};
+
+ // For every "future" version v, check if the check if the corresponding
+ // spelling of id was introduced later than the current version. If so,
+ // and if that spelling matches the source spelling, issue a warning.
+ for (unsigned v : llvm::omp::getOpenMPVersions()) {
+ if (v <= version) {
+ continue;
+ }
+ llvm::StringRef name{llvm::omp::getOpenMPDirectiveName(id, v)};
+ auto [kind, versions]{llvm::omp::getOpenMPDirectiveKindAndVersions(name)};
+ assert(kind == id && "Directive kind mismatch");
+
+ if (static_cast<int>(version) >= versions.Min) {
+ continue;
+ }
+ if (ref == removeSpaces(name)) {
+ context_.Say(spelling,
----------------
Stylie777 wrote:
Makes sense, thanks for the explanation
https://github.com/llvm/llvm-project/pull/147765
More information about the flang-commits
mailing list