[clang] 45e01ce - [clang] Avoid suggesting typoed directives in `.S` files

Nick Desaulniers via cfe-commits cfe-commits at lists.llvm.org
Mon May 16 15:47:05 PDT 2022


Author: Ken Matsui
Date: 2022-05-16T15:46:59-07:00
New Revision: 45e01ce5fe6a5e4dc25ffdf626caa344fbcb93dd

URL: https://github.com/llvm/llvm-project/commit/45e01ce5fe6a5e4dc25ffdf626caa344fbcb93dd
DIFF: https://github.com/llvm/llvm-project/commit/45e01ce5fe6a5e4dc25ffdf626caa344fbcb93dd.diff

LOG: [clang] Avoid suggesting typoed directives in `.S` files

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

Added: 
    clang/test/Preprocessor/suggest-typoed-directive.S

Modified: 
    clang/lib/Lex/PPDirectives.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 55045320fd591..d947c1580f5ca 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -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
index 0000000000000..d00c21f621fa8
--- /dev/null
+++ b/clang/test/Preprocessor/suggest-typoed-directive.S
@@ -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


        


More information about the cfe-commits mailing list