[clang] Avoid suggesting typoed directives in `.S` files
authorKen Matsui <vcs@kmatsui.me>
Mon, 16 May 2022 22:36:54 +0000 (15:36 -0700)
committerNick Desaulniers <ndesaulniers@google.com>
Mon, 16 May 2022 22:46:59 +0000 (15:46 -0700)
This patch is itended to avoid suggesting typoed directives in `.S`
files to support the cases of `#` directives treated as comments or
various pseudo-ops. The feature is implemented in
https://reviews.llvm.org/D124726.

Fixes: https://reviews.llvm.org/D124726#3516346.

Reviewed By: nickdesaulniers

Differential Revision: https://reviews.llvm.org/D125727

clang/lib/Lex/PPDirectives.cpp
clang/test/Preprocessor/suggest-typoed-directive.S [new file with mode: 0644]

index 5504532..d947c15 100644 (file)
@@ -481,6 +481,10 @@ Optional<unsigned> Preprocessor::getSkippedRangeForExcludedConditionalBlock(
 void Preprocessor::SuggestTypoedDirective(const Token &Tok,
                                           StringRef Directive,
                                           const SourceLocation &EndLoc) const {
+  // If this is a `.S` file, treat unknown # directives as non-preprocessor
+  // directives.
+  if (getLangOpts().AsmPreprocessor) return;
+
   std::vector<StringRef> Candidates = {
       "if", "ifdef", "ifndef", "elif", "else", "endif"
   };
diff --git a/clang/test/Preprocessor/suggest-typoed-directive.S b/clang/test/Preprocessor/suggest-typoed-directive.S
new file mode 100644 (file)
index 0000000..d00c21f
--- /dev/null
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+#ifdef UNDEFINED
+#id
+#ifd
+#ifde
+#elf
+#elsif
+#elseif
+#elfidef
+#elfindef
+#elfinndef
+#els
+#endi
+#endif
+
+#ifdef UNDEFINED
+# in in order to perform
+#endif
+
+#ifdef UNDEFINED
+#i
+#endif
+
+#if special_compiler
+#special_compiler_directive
+#endif