[clang] 26f8e14 - [OpenACC] NFC: Stop using 'getSpelling' while parsing OpenACC

via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 6 07:12:06 PST 2023


Author: erichkeane
Date: 2023-12-06T07:12:01-08:00
New Revision: 26f8e1461e24bd71dcd409d1fe88ddc1c7cf5da1

URL: https://github.com/llvm/llvm-project/commit/26f8e1461e24bd71dcd409d1fe88ddc1c7cf5da1
DIFF: https://github.com/llvm/llvm-project/commit/26f8e1461e24bd71dcd409d1fe88ddc1c7cf5da1.diff

LOG: [OpenACC] NFC: Stop using 'getSpelling' while parsing OpenACC

It was brought up during the cache review that we shouldn't be using
'getSpelling', and instead should use the IdentifierInfo itself.  This
patch replaces all uses of it.

Added: 
    

Modified: 
    clang/include/clang/Basic/DiagnosticParseKinds.td
    clang/lib/Parse/ParseOpenACC.cpp
    clang/test/ParserOpenACC/parse-constructs.c
    clang/test/ParserOpenACC/unimplemented.c
    clang/test/ParserOpenACC/unimplemented.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 21fe6066d5876..e0e199e0d7300 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -1362,7 +1362,7 @@ def warn_pragma_acc_unimplemented_clause_parsing
     : Warning<"OpenACC clause parsing not yet implemented">,
       InGroup<SourceUsesOpenACC>;
 def err_acc_invalid_directive
-    : Error<"invalid OpenACC directive '%select{%1|%1 %2}0'">;
+    : Error<"invalid OpenACC directive %select{%1|'%1 %2'}0">;
 def err_acc_missing_directive : Error<"expected OpenACC directive">;
 def err_acc_invalid_open_paren
     : Error<"expected clause-list or newline in OpenACC directive">;

diff  --git a/clang/lib/Parse/ParseOpenACC.cpp b/clang/lib/Parse/ParseOpenACC.cpp
index fb292f270d916..71cb665a56327 100644
--- a/clang/lib/Parse/ParseOpenACC.cpp
+++ b/clang/lib/Parse/ParseOpenACC.cpp
@@ -36,9 +36,12 @@ enum class OpenACCDirectiveKindEx {
 // identifies the first token), and doesn't fully handle 'enter data', 'exit
 // data', nor any of the 'atomic' variants, just the first token of each.  So
 // this should only be used by `ParseOpenACCDirectiveKind`.
-OpenACCDirectiveKindEx getOpenACCDirectiveKind(StringRef Name) {
+OpenACCDirectiveKindEx getOpenACCDirectiveKind(Token Tok) {
+  if (!Tok.is(tok::identifier))
+    return OpenACCDirectiveKindEx::Invalid;
   OpenACCDirectiveKind DirKind =
-      llvm::StringSwitch<OpenACCDirectiveKind>(Name)
+      llvm::StringSwitch<OpenACCDirectiveKind>(
+          Tok.getIdentifierInfo()->getName())
           .Case("parallel", OpenACCDirectiveKind::Parallel)
           .Case("serial", OpenACCDirectiveKind::Serial)
           .Case("kernels", OpenACCDirectiveKind::Kernels)
@@ -58,7 +61,8 @@ OpenACCDirectiveKindEx getOpenACCDirectiveKind(StringRef Name) {
   if (DirKind != OpenACCDirectiveKind::Invalid)
     return static_cast<OpenACCDirectiveKindEx>(DirKind);
 
-  return llvm::StringSwitch<OpenACCDirectiveKindEx>(Name)
+  return llvm::StringSwitch<OpenACCDirectiveKindEx>(
+             Tok.getIdentifierInfo()->getName())
       .Case("enter", OpenACCDirectiveKindEx::Enter)
       .Case("exit", OpenACCDirectiveKindEx::Exit)
       .Default(OpenACCDirectiveKindEx::Invalid);
@@ -66,8 +70,11 @@ OpenACCDirectiveKindEx getOpenACCDirectiveKind(StringRef Name) {
 
 // Since 'atomic' is effectively a compound directive, this will decode the
 // second part of the directive.
-OpenACCAtomicKind getOpenACCAtomicKind(StringRef Name) {
-  return llvm::StringSwitch<OpenACCAtomicKind>(Name)
+OpenACCAtomicKind getOpenACCAtomicKind(Token Tok) {
+  if (!Tok.is(tok::identifier))
+    return OpenACCAtomicKind::Invalid;
+  return llvm::StringSwitch<OpenACCAtomicKind>(
+             Tok.getIdentifierInfo()->getName())
       .Case("read", OpenACCAtomicKind::Read)
       .Case("write", OpenACCAtomicKind::Write)
       .Case("update", OpenACCAtomicKind::Update)
@@ -75,22 +82,25 @@ OpenACCAtomicKind getOpenACCAtomicKind(StringRef Name) {
       .Default(OpenACCAtomicKind::Invalid);
 }
 
-bool isOpenACCDirectiveKind(OpenACCDirectiveKind Kind, StringRef Tok) {
+bool isOpenACCDirectiveKind(OpenACCDirectiveKind Kind, Token Tok) {
+  if (!Tok.is(tok::identifier))
+    return false;
+
   switch (Kind) {
   case OpenACCDirectiveKind::Parallel:
-    return Tok == "parallel";
+    return Tok.getIdentifierInfo()->isStr("parallel");
   case OpenACCDirectiveKind::Serial:
-    return Tok == "serial";
+    return Tok.getIdentifierInfo()->isStr("serial");
   case OpenACCDirectiveKind::Kernels:
-    return Tok == "kernels";
+    return Tok.getIdentifierInfo()->isStr("kernels");
   case OpenACCDirectiveKind::Data:
-    return Tok == "data";
+    return Tok.getIdentifierInfo()->isStr("data");
   case OpenACCDirectiveKind::HostData:
-    return Tok == "host_data";
+    return Tok.getIdentifierInfo()->isStr("host_data");
   case OpenACCDirectiveKind::Loop:
-    return Tok == "loop";
+    return Tok.getIdentifierInfo()->isStr("loop");
   case OpenACCDirectiveKind::Cache:
-    return Tok == "cache";
+    return Tok.getIdentifierInfo()->isStr("cache");
 
   case OpenACCDirectiveKind::ParallelLoop:
   case OpenACCDirectiveKind::SerialLoop:
@@ -100,19 +110,19 @@ bool isOpenACCDirectiveKind(OpenACCDirectiveKind Kind, StringRef Tok) {
     return false;
 
   case OpenACCDirectiveKind::Atomic:
-    return Tok == "atomic";
+    return Tok.getIdentifierInfo()->isStr("atomic");
   case OpenACCDirectiveKind::Routine:
-    return Tok == "routine";
+    return Tok.getIdentifierInfo()->isStr("routine");
   case OpenACCDirectiveKind::Declare:
-    return Tok == "declare";
+    return Tok.getIdentifierInfo()->isStr("declare");
   case OpenACCDirectiveKind::Init:
-    return Tok == "init";
+    return Tok.getIdentifierInfo()->isStr("init");
   case OpenACCDirectiveKind::Shutdown:
-    return Tok == "shutdown";
+    return Tok.getIdentifierInfo()->isStr("shutdown");
   case OpenACCDirectiveKind::Set:
-    return Tok == "set";
+    return Tok.getIdentifierInfo()->isStr("set");
   case OpenACCDirectiveKind::Update:
-    return Tok == "update";
+    return Tok.getIdentifierInfo()->isStr("update");
   case OpenACCDirectiveKind::Invalid:
     return false;
   }
@@ -121,20 +131,22 @@ bool isOpenACCDirectiveKind(OpenACCDirectiveKind Kind, StringRef Tok) {
 
 OpenACCDirectiveKind
 ParseOpenACCEnterExitDataDirective(Parser &P, Token FirstTok,
-                                   StringRef FirstTokSpelling,
                                    OpenACCDirectiveKindEx ExtDirKind) {
   Token SecondTok = P.getCurToken();
 
   if (SecondTok.isAnnotation()) {
-    P.Diag(FirstTok, diag::err_acc_invalid_directive) << 0 << FirstTokSpelling;
+    P.Diag(FirstTok, diag::err_acc_invalid_directive)
+        << 0 << FirstTok.getIdentifierInfo();
     return OpenACCDirectiveKind::Invalid;
   }
 
-  std::string SecondTokSpelling = P.getPreprocessor().getSpelling(SecondTok);
-
-  if (!isOpenACCDirectiveKind(OpenACCDirectiveKind::Data, SecondTokSpelling)) {
-    P.Diag(FirstTok, diag::err_acc_invalid_directive)
-        << 1 << FirstTokSpelling << SecondTokSpelling;
+  if (!isOpenACCDirectiveKind(OpenACCDirectiveKind::Data, SecondTok)) {
+    if (!SecondTok.is(tok::identifier))
+      P.Diag(SecondTok, diag::err_expected) << tok::identifier;
+    else
+      P.Diag(FirstTok, diag::err_acc_invalid_directive)
+          << 1 << FirstTok.getIdentifierInfo()->getName()
+          << SecondTok.getIdentifierInfo()->getName();
     return OpenACCDirectiveKind::Invalid;
   }
 
@@ -152,9 +164,7 @@ OpenACCAtomicKind ParseOpenACCAtomicKind(Parser &P) {
   if (AtomicClauseToken.isAnnotation())
     return OpenACCAtomicKind::Update;
 
-  std::string AtomicClauseSpelling =
-      P.getPreprocessor().getSpelling(AtomicClauseToken);
-  OpenACCAtomicKind AtomicKind = getOpenACCAtomicKind(AtomicClauseSpelling);
+  OpenACCAtomicKind AtomicKind = getOpenACCAtomicKind(AtomicClauseToken);
 
   // If we don't know what this is, treat it as 'nothing', and treat the rest of
   // this as a clause list, which, despite being invalid, is likely what the
@@ -178,9 +188,8 @@ OpenACCDirectiveKind ParseOpenACCDirectiveKind(Parser &P) {
   }
 
   P.ConsumeToken();
-  std::string FirstTokSpelling = P.getPreprocessor().getSpelling(FirstTok);
 
-  OpenACCDirectiveKindEx ExDirKind = getOpenACCDirectiveKind(FirstTokSpelling);
+  OpenACCDirectiveKindEx ExDirKind = getOpenACCDirectiveKind(FirstTok);
 
   // OpenACCDirectiveKindEx is meant to be an extended list
   // over OpenACCDirectiveKind, so any value below Invalid is one of the
@@ -190,14 +199,17 @@ OpenACCDirectiveKind ParseOpenACCDirectiveKind(Parser &P) {
   // immediately cast it and use it as that.
   if (ExDirKind >= OpenACCDirectiveKindEx::Invalid) {
     switch (ExDirKind) {
-    case OpenACCDirectiveKindEx::Invalid:
-      P.Diag(FirstTok, diag::err_acc_invalid_directive)
-          << 0 << FirstTokSpelling;
+    case OpenACCDirectiveKindEx::Invalid: {
+      if (!FirstTok.is(tok::identifier))
+        P.Diag(FirstTok, diag::err_expected) << tok::identifier;
+      else
+        P.Diag(FirstTok, diag::err_acc_invalid_directive)
+            << 0 << FirstTok.getIdentifierInfo();
       return OpenACCDirectiveKind::Invalid;
+    }
     case OpenACCDirectiveKindEx::Enter:
     case OpenACCDirectiveKindEx::Exit:
-      return ParseOpenACCEnterExitDataDirective(P, FirstTok, FirstTokSpelling,
-                                                ExDirKind);
+      return ParseOpenACCEnterExitDataDirective(P, FirstTok, ExDirKind);
     }
   }
 
@@ -208,8 +220,7 @@ OpenACCDirectiveKind ParseOpenACCDirectiveKind(Parser &P) {
   // clause.
   Token SecondTok = P.getCurToken();
   if (!SecondTok.isAnnotation() &&
-      isOpenACCDirectiveKind(OpenACCDirectiveKind::Loop,
-                             P.getPreprocessor().getSpelling(SecondTok))) {
+      isOpenACCDirectiveKind(OpenACCDirectiveKind::Loop, SecondTok)) {
     switch (DirKind) {
     default:
       // Nothing to do except in the below cases, as they should be diagnosed as

diff  --git a/clang/test/ParserOpenACC/parse-constructs.c b/clang/test/ParserOpenACC/parse-constructs.c
index f0f9d75ade1fb..b745f54bd715c 100644
--- a/clang/test/ParserOpenACC/parse-constructs.c
+++ b/clang/test/ParserOpenACC/parse-constructs.c
@@ -61,7 +61,7 @@ void func() {
   // expected-warning at +1{{OpenACC directives not yet implemented, pragma ignored}}
 #pragma acc enter
   for(;;){}
-  // expected-error at +3{{invalid OpenACC directive 'exit }'}}
+  // expected-error at +3{{expected identifier}}
   // expected-warning at +2{{OpenACC clause parsing not yet implemented}}
   // expected-warning at +1{{OpenACC directives not yet implemented, pragma ignored}}
 #pragma acc exit }

diff  --git a/clang/test/ParserOpenACC/unimplemented.c b/clang/test/ParserOpenACC/unimplemented.c
index dd2e8bea74709..2927e685d30b5 100644
--- a/clang/test/ParserOpenACC/unimplemented.c
+++ b/clang/test/ParserOpenACC/unimplemented.c
@@ -1,26 +1,26 @@
 // RUN: %clang_cc1 %s -verify -fopenacc
 
 // Parser::ParseExternalDeclaration
-// expected-error at +3{{invalid OpenACC directive 'not'}}
+// expected-error at +3{{invalid OpenACC directive 'havent'}}
 // expected-warning at +2{{OpenACC clause parsing not yet implemented}}
 // expected-warning at +1{{OpenACC directives not yet implemented, pragma ignored}}
-#pragma acc not yet implemented
+#pragma acc havent implemented
 int foo;
 
 struct S {
 // Parser::ParseStructUnionBody
-// expected-error at +3{{invalid OpenACC directive 'not'}}
+// expected-error at +3{{invalid OpenACC directive 'havent'}}
 // expected-warning at +2{{OpenACC clause parsing not yet implemented}}
 // expected-warning at +1{{OpenACC directives not yet implemented, pragma ignored}}
-#pragma acc not yet implemented
+#pragma acc havent implemented
   int foo;
 };
 
 void func() {
 // Parser::ParseStmtOrDeclarationAfterAttributes
-// expected-error at +3{{invalid OpenACC directive 'not'}}
+// expected-error at +3{{invalid OpenACC directive 'havent'}}
 // expected-warning at +2{{OpenACC clause parsing not yet implemented}}
 // expected-warning at +1{{OpenACC directives not yet implemented, pragma ignored}}
-#pragma acc not yet implemented
+#pragma acc havent implemented
   while(0) {}
 }

diff  --git a/clang/test/ParserOpenACC/unimplemented.cpp b/clang/test/ParserOpenACC/unimplemented.cpp
index 4f6c5a649065e..77619c9ae6551 100644
--- a/clang/test/ParserOpenACC/unimplemented.cpp
+++ b/clang/test/ParserOpenACC/unimplemented.cpp
@@ -1,26 +1,26 @@
 // RUN: %clang_cc1 %s -verify -fopenacc
 
 // Parser::ParseExternalDeclaration
-// expected-error at +3{{invalid OpenACC directive 'not'}}
+// expected-error at +3{{invalid OpenACC directive 'havent'}}
 // expected-warning at +2{{OpenACC clause parsing not yet implemented}}
 // expected-warning at +1{{OpenACC directives not yet implemented, pragma ignored}}
-#pragma acc not yet implemented
+#pragma acc havent implemented
 int foo;
 
 struct S {
 // Parser::ParseCXXClassMemberDeclarationWithPragmas
-// expected-error at +3{{invalid OpenACC directive 'not'}}
+// expected-error at +3{{invalid OpenACC directive 'havent'}}
 // expected-warning at +2{{OpenACC clause parsing not yet implemented}}
 // expected-warning at +1{{OpenACC directives not yet implemented, pragma ignored}}
-#pragma acc not yet implemented
+#pragma acc havent implemented
   int foo;
 };
 
 void func() {
 // Parser::ParseStmtOrDeclarationAfterAttributes
-// expected-error at +3{{invalid OpenACC directive 'not'}}
+// expected-error at +3{{invalid OpenACC directive 'havent'}}
 // expected-warning at +2{{OpenACC clause parsing not yet implemented}}
 // expected-warning at +1{{OpenACC directives not yet implemented, pragma ignored}}
-#pragma acc not yet implemented
+#pragma acc havent implemented
   while(false) {}
 }


        


More information about the cfe-commits mailing list