r256595 - Emit a -Wmicrosoft warning when pasting /##/ into a comment token in MS mode.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 29 15:16:23 PST 2015


On Tue, Dec 29, 2015 at 3:06 PM, Nico Weber via cfe-commits <
cfe-commits at lists.llvm.org> wrote:

> Author: nico
> Date: Tue Dec 29 17:06:17 2015
> New Revision: 256595
>
> URL: http://llvm.org/viewvc/llvm-project?rev=256595&view=rev
> Log:
> Emit a -Wmicrosoft warning when pasting /##/ into a comment token in MS
> mode.
>
> Modified:
>     cfe/trunk/include/clang/Basic/DiagnosticGroups.td
>     cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td
>     cfe/trunk/include/clang/Lex/TokenLexer.h
>     cfe/trunk/lib/Lex/PPLexerChange.cpp
>     cfe/trunk/lib/Lex/TokenLexer.cpp
>     cfe/trunk/test/Preprocessor/macro_paste_msextensions.c
>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=256595&r1=256594&r2=256595&view=diff
>
> ==============================================================================
> --- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
> +++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Tue Dec 29 17:06:17
> 2015
> @@ -765,6 +765,7 @@ def MicrosoftCast : DiagGroup<"microsoft
>  def MicrosoftConstInit : DiagGroup<"microsoft-const-init">;
>  def MicrosoftVoidPseudoDtor : DiagGroup<"microsoft-void-pseudo-dtor">;
>  def MicrosoftAnonTag : DiagGroup<"microsoft-anon-tag">;
> +def MicrosoftCommentPaste : DiagGroup<"microsoft-comment-paste">;
>  // Aliases.
>  def : DiagGroup<"msvc-include", [MicrosoftInclude]>;
>                  // -Wmsvc-include = -Wmicrosoft-include
> @@ -778,7 +779,8 @@ def Microsoft : DiagGroup<"microsoft",
>       MicrosoftEnumValue, MicrosoftDefaultArgRedefinition,
> MicrosoftTemplate,
>       MicrosoftRedeclareStatic, MicrosoftEnumForwardReference,
> MicrosoftGoto,
>       MicrosoftFlexibleArray, MicrosoftExtraQualification, MicrosoftCast,
> -     MicrosoftConstInit, MicrosoftVoidPseudoDtor, MicrosoftAnonTag]>;
> +     MicrosoftConstInit, MicrosoftVoidPseudoDtor, MicrosoftAnonTag,
> +     MicrosoftCommentPaste]>;
>
>  def ObjCNonUnifiedException : DiagGroup<"objc-nonunified-exceptions">;
>
>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td?rev=256595&r1=256594&r2=256595&view=diff
>
> ==============================================================================
> --- cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td (original)
> +++ cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td Tue Dec 29
> 17:06:17 2015
> @@ -59,6 +59,9 @@ def ext_dollar_in_identifier : Extension
>  def ext_charize_microsoft : Extension<
>    "charizing operator #@ is a Microsoft extension">,
>    InGroup<MicrosoftCharize>;
> +def ext_comment_paste_microsoft : Extension<
> +  "pasting two '/' tokens into a '//' comment token is a Microsoft
> extension">,
>

It's not really technically correct to call this a token. Maybe drop the
word "token" here?


> +  InGroup<MicrosoftCommentPaste>;
>
>  def ext_token_used : Extension<"extension used">,
>    InGroup<DiagGroup<"language-extension-token">>;
>
> Modified: cfe/trunk/include/clang/Lex/TokenLexer.h
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/TokenLexer.h?rev=256595&r1=256594&r2=256595&view=diff
>
> ==============================================================================
> --- cfe/trunk/include/clang/Lex/TokenLexer.h (original)
> +++ cfe/trunk/include/clang/Lex/TokenLexer.h Tue Dec 29 17:06:17 2015
> @@ -175,7 +175,7 @@ private:
>    /// macro, other active macros, and anything left on the current
> physical
>    /// source line of the expanded buffer.  Handle this by returning the
>    /// first token on the next line.
> -  void HandleMicrosoftCommentPaste(Token &Tok);
> +  void HandleMicrosoftCommentPaste(Token &Tok, SourceLocation OpLoc);
>
>    /// \brief If \p loc is a FileID and points inside the current macro
>    /// definition, returns the appropriate source location pointing at the
>
> Modified: cfe/trunk/lib/Lex/PPLexerChange.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPLexerChange.cpp?rev=256595&r1=256594&r2=256595&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/Lex/PPLexerChange.cpp (original)
> +++ cfe/trunk/lib/Lex/PPLexerChange.cpp Tue Dec 29 17:06:17 2015
> @@ -561,7 +561,6 @@ void Preprocessor::RemoveTopOfLexerStack
>  void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) {
>    assert(CurTokenLexer && !CurPPLexer &&
>           "Pasted comment can only be formed from macro");
> -
>    // We handle this by scanning for the closest real lexer, switching it
> to
>    // raw mode and preprocessor mode.  This will cause it to return \n as
> an
>    // explicit EOD token.
>
> Modified: cfe/trunk/lib/Lex/TokenLexer.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/TokenLexer.cpp?rev=256595&r1=256594&r2=256595&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/Lex/TokenLexer.cpp (original)
> +++ cfe/trunk/lib/Lex/TokenLexer.cpp Tue Dec 29 17:06:17 2015
> @@ -624,21 +624,22 @@ bool TokenLexer::PasteTokens(Token &Tok)
>        // error.  This occurs with "x ## +"  and other stuff.  Return with
> Tok
>        // unmodified and with RHS as the next token to lex.
>        if (isInvalid) {
> +        // Explicitly convert the token location to have proper expansion
> +        // information so that the user knows where it came from.
> +        SourceManager &SM = PP.getSourceManager();
> +        SourceLocation Loc =
> +          SM.createExpansionLoc(PasteOpLoc, ExpandLocStart, ExpandLocEnd,
> 2);
> +
>          // Test for the Microsoft extension of /##/ turning into // here
> on the
>          // error path.
>          if (PP.getLangOpts().MicrosoftExt && Tok.is(tok::slash) &&
>              RHS.is(tok::slash)) {
> -          HandleMicrosoftCommentPaste(Tok);
> +          HandleMicrosoftCommentPaste(Tok, Loc);
>            return true;
>          }
>
>          // Do not emit the error when preprocessing assembler code.
>          if (!PP.getLangOpts().AsmPreprocessor) {
> -          // Explicitly convert the token location to have proper
> expansion
> -          // information so that the user knows where it came from.
> -          SourceManager &SM = PP.getSourceManager();
> -          SourceLocation Loc =
> -            SM.createExpansionLoc(PasteOpLoc, ExpandLocStart,
> ExpandLocEnd, 2);
>            // If we're in microsoft extensions mode, downgrade this from a
> hard
>            // error to an extension that defaults to an error.  This allows
>            // disabling it.
> @@ -719,7 +720,9 @@ bool TokenLexer::isParsingPreprocessorDi
>  /// macro, other active macros, and anything left on the current physical
>  /// source line of the expanded buffer.  Handle this by returning the
>  /// first token on the next line.
> -void TokenLexer::HandleMicrosoftCommentPaste(Token &Tok) {
> +void TokenLexer::HandleMicrosoftCommentPaste(Token &Tok, SourceLocation
> OpLoc) {
> +  PP.Diag(OpLoc, diag::ext_comment_paste_microsoft);
> +
>    // We 'comment out' the rest of this macro by just ignoring the rest of
> the
>    // tokens that have not been lexed yet, if any.
>
>
> Modified: cfe/trunk/test/Preprocessor/macro_paste_msextensions.c
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/macro_paste_msextensions.c?rev=256595&r1=256594&r2=256595&view=diff
>
> ==============================================================================
> --- cfe/trunk/test/Preprocessor/macro_paste_msextensions.c (original)
> +++ cfe/trunk/test/Preprocessor/macro_paste_msextensions.c Tue Dec 29
> 17:06:17 2015
> @@ -1,3 +1,4 @@
> +// RUN: %clang_cc1 -verify -fms-extensions -Wmicrosoft %s
>  // RUN: not %clang_cc1 -P -E -fms-extensions %s | FileCheck
> -strict-whitespace %s
>
>  // This horrible stuff should preprocess into (other than whitespace):
> @@ -10,6 +11,7 @@ int foo;
>  // CHECK: int foo;
>
>  #define comment /##/  dead tokens live here
> +// expected-warning at +1 {{pasting two '/' tokens}}
>  comment This is stupidity
>
>  int bar;
> @@ -18,6 +20,7 @@ int bar;
>
>  #define nested(x) int x comment cute little dead tokens...
>
> +// expected-warning at +1 {{pasting two '/' tokens}}
>  nested(baz)  rise of the dead tokens
>
>  ;
> @@ -29,13 +32,13 @@ nested(baz)  rise of the dead tokens
>  // rdar://8197149 - VC++ allows invalid token pastes: (##baz
>  #define foo(x) abc(x)
>  #define bar(y) foo(##baz(y))
> -bar(q)
> +bar(q) // expected-warning {{type specifier missing}} expected-error
> {{invalid preprocessing token}} expected-error {{parameter list without
> types}}
>
>  // CHECK: abc(baz(q))
>
>
>  #define str(x) #x
>  #define collapse_spaces(a, b, c, d) str(a ## - ## b ## - ## c ## d)
> -collapse_spaces(1a, b2, 3c, d4)
> +collapse_spaces(1a, b2, 3c, d4) // expected-error 4 {{invalid
> preprocessing token}} expected-error {{expected function body}}
>
>  // CHECK: "1a-b2-3cd4"
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20151229/d85c89cc/attachment-0001.html>


More information about the cfe-commits mailing list