From: Nico Weber Date: Tue, 29 Dec 2015 23:06:17 +0000 (+0000) Subject: Emit a -Wmicrosoft warning when pasting /##/ into a comment token in MS mode. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=87455cb66c8b9e8d0311e38e013fb1629ac6ea99;p=clang Emit a -Wmicrosoft warning when pasting /##/ into a comment token in MS mode. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@256595 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticGroups.td b/include/clang/Basic/DiagnosticGroups.td index 44a6a114ed..32353b51ee 100644 --- a/include/clang/Basic/DiagnosticGroups.td +++ b/include/clang/Basic/DiagnosticGroups.td @@ -765,6 +765,7 @@ def MicrosoftCast : DiagGroup<"microsoft-cast">; 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">; diff --git a/include/clang/Basic/DiagnosticLexKinds.td b/include/clang/Basic/DiagnosticLexKinds.td index 38599d6b1f..2ed40c0cde 100644 --- a/include/clang/Basic/DiagnosticLexKinds.td +++ b/include/clang/Basic/DiagnosticLexKinds.td @@ -59,6 +59,9 @@ def ext_dollar_in_identifier : Extension<"'$' in identifier">, def ext_charize_microsoft : Extension< "charizing operator #@ is a Microsoft extension">, InGroup; +def ext_comment_paste_microsoft : Extension< + "pasting two '/' tokens into a '//' comment token is a Microsoft extension">, + InGroup; def ext_token_used : Extension<"extension used">, InGroup>; diff --git a/include/clang/Lex/TokenLexer.h b/include/clang/Lex/TokenLexer.h index 31197367c4..fdeed44d8f 100644 --- a/include/clang/Lex/TokenLexer.h +++ b/include/clang/Lex/TokenLexer.h @@ -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 diff --git a/lib/Lex/PPLexerChange.cpp b/lib/Lex/PPLexerChange.cpp index 7c0d55e1f0..2f09841c5b 100644 --- a/lib/Lex/PPLexerChange.cpp +++ b/lib/Lex/PPLexerChange.cpp @@ -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. diff --git a/lib/Lex/TokenLexer.cpp b/lib/Lex/TokenLexer.cpp index e7512fa283..c42966928e 100644 --- a/lib/Lex/TokenLexer.cpp +++ b/lib/Lex/TokenLexer.cpp @@ -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::isParsingPreprocessorDirective() const { /// 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. diff --git a/test/Preprocessor/macro_paste_msextensions.c b/test/Preprocessor/macro_paste_msextensions.c index aa5f41f9ee..dcc5336b91 100644 --- a/test/Preprocessor/macro_paste_msextensions.c +++ b/test/Preprocessor/macro_paste_msextensions.c @@ -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@+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@+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"