[clang] [clang][ScanDeps] Canonicalize -D and -U flags (PR #82298)

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 19 19:20:21 PST 2024


================
@@ -179,6 +179,73 @@ static void sanitizeDiagOpts(DiagnosticOptions &DiagOpts) {
   DiagOpts.IgnoreWarnings = true;
 }
 
+// Clang implements -D and -U by splatting text into a predefines buffer. This
+// allows constructs such as `-DFඞ=3 "-D F\u{0D9E} 4 3 2”` to be accepted and
+// define the same macro, or adding C++ style comments before the macro name.
+//
+// This function checks that the first non-space characters in the macro
+// obviously form an identifier that can be uniqued on without lexing. Failing
+// to do this could lead to changing the final definition of a macro.
+//
+// We could set up a preprocessor and actually lex the name, but that's very
+// heavyweight for a situation that will almost never happen in practice.
+static std::optional<StringRef> getSimpleMacroName(StringRef Macro) {
+  StringRef Name = Macro.split("=").first.trim(" \t");
+  std::size_t I = 0;
+  for (; I != Name.size(); ++I) {
+    switch (Name[I]) {
+    case '(': // Start of macro parameter list
+    case ' ': // End of macro name
+    case '\t':
+      goto EndOfMacro;
----------------
jansvoboda11 wrote:

I'd prefer making `EndOfMacro` a lambda and calling it here and after the loop.

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


More information about the cfe-commits mailing list