[llvm] [TableGen] Detect invalid -D arguments and fail (PR #102813)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 19 09:54:15 PDT 2024


================
@@ -44,6 +44,25 @@ constexpr PreprocessorDir PreprocessorDirs[] = {{tgtok::Ifdef, "ifdef"},
                                                 {tgtok::Endif, "endif"},
                                                 {tgtok::Define, "define"}};
 
+// Returns a pointer past the end of a valid macro name at the start of `Str`.
+// Valid macro names match the regular expression [a-zA-Z_][0-9a-zA-Z_]*.
+static const char *lexMacroName(StringRef Str) {
+  assert(!Str.empty());
+
+  // Macro names start with [a-zA-Z_].
+  const char *Next = Str.begin();
+  if (*Next != '_' && !isalpha(*Next))
+    return Next;
+  // Eat the first character of the name.
+  ++Next;
+
+  // Match the rest of the identifier regex: [0-9a-zA-Z_]*
+  const char *End = Str.end();
+  while (Next != End && (isalpha(*Next) || isdigit(*Next) || *Next == '_'))
----------------
MaskRay wrote:

use `isAlnum` from llvm/ADT/StringExtras.h to avoid possibly locale dependent behavior (e.g. glibc).

https://github.com/llvm/llvm-project/pull/102813


More information about the llvm-commits mailing list