From f59e17ecf06ac60065e2d02058bd6f21f5d216cc Mon Sep 17 00:00:00 2001 From: Steve Naroff Date: Wed, 24 Dec 2008 20:59:21 +0000 Subject: [PATCH] Add explicit "fuzzy" parse support for Microsoft declspec. Remove previous __declspec macro that would effectively erase the construct prior to parsing. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61422 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/TokenKinds.def | 3 +++ include/clang/Parse/Parser.h | 1 + lib/Lex/Preprocessor.cpp | 1 - lib/Parse/ParseDecl.cpp | 21 +++++++++++++++++++++ lib/Parse/ParseDeclCXX.cpp | 4 ++++ 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/include/clang/Basic/TokenKinds.def b/include/clang/Basic/TokenKinds.def index 150a2eb13c..c0a448dfa7 100644 --- a/include/clang/Basic/TokenKinds.def +++ b/include/clang/Basic/TokenKinds.def @@ -307,6 +307,9 @@ KEYWORD(__thread , EXTC90|EXTC99|EXTCPP|EXTCPP0x) // Apple Extension. KEYWORD(__private_extern__ , EXTC90|EXTC99|NOTCPP) +// Microsoft Extension. +KEYWORD(__declspec , EXTC90|EXTC99|EXTCPP|EXTCPP0x) + // Alternate spelling for various tokens. There are GCC extensions in all // languages, but should not be disabled in strict conformance mode. ALIAS("__attribute__", __attribute) diff --git a/include/clang/Parse/Parser.h b/include/clang/Parse/Parser.h index df6f895e49..28aed8b58a 100644 --- a/include/clang/Parse/Parser.h +++ b/include/clang/Parse/Parser.h @@ -887,6 +887,7 @@ private: TypeTy *ParseTypeName(); AttributeList *ParseAttributes(); + void FuzzyParseMicrosoftDeclSpec(); void ParseTypeofSpecifier(DeclSpec &DS); /// DeclaratorScopeObj - RAII object used in Parser::ParseDirectDeclarator to diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp index 7800fb5189..97ff07049f 100644 --- a/lib/Lex/Preprocessor.cpp +++ b/lib/Lex/Preprocessor.cpp @@ -493,7 +493,6 @@ static void InitializePredefinedMacros(Preprocessor &PP, DefineBuiltinMacro(Buf, "__int16=short"); DefineBuiltinMacro(Buf, "__int32=int"); DefineBuiltinMacro(Buf, "__int64=long long"); - DefineBuiltinMacro(Buf, "__declspec(X)="); } diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index c7f92cf014..a8052dc290 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -192,6 +192,20 @@ AttributeList *Parser::ParseAttributes() { return CurrAttr; } +/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this +/// routine is called to skip/ignore tokens that comprise the MS declspec. +void Parser::FuzzyParseMicrosoftDeclSpec() { + assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); + ConsumeToken(); + if (Tok.is(tok::l_paren)) { + unsigned short savedParenCount = ParenCount; + do { + ConsumeAnyToken(); + } while (ParenCount > savedParenCount && Tok.isNot(tok::eof)); + } + return; +} + /// ParseDeclaration - Parse a full 'declaration', which consists of /// declaration-specifiers, some number of declarators, and a semicolon. /// 'Context' should be a Declarator::TheContext value. @@ -538,6 +552,13 @@ void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, case tok::kw___attribute: DS.AddAttributes(ParseAttributes()); continue; + + // Microsoft declspec support. + case tok::kw___declspec: + if (!PP.getLangOptions().Microsoft) + goto DoneWithDeclSpec; + FuzzyParseMicrosoftDeclSpec(); + continue; // storage-class-specifier case tok::kw_typedef: diff --git a/lib/Parse/ParseDeclCXX.cpp b/lib/Parse/ParseDeclCXX.cpp index 1b0b811c88..1d3de90060 100644 --- a/lib/Parse/ParseDeclCXX.cpp +++ b/lib/Parse/ParseDeclCXX.cpp @@ -220,6 +220,10 @@ void Parser::ParseClassSpecifier(DeclSpec &DS, if (Tok.is(tok::kw___attribute)) Attr = ParseAttributes(); + // If declspecs exist after tag, parse them. + if (Tok.is(tok::kw___declspec) && PP.getLangOptions().Microsoft) + FuzzyParseMicrosoftDeclSpec(); + // Parse the (optional) nested-name-specifier. CXXScopeSpec SS; if (getLang().CPlusPlus && MaybeParseCXXScopeSpecifier(SS)) { -- 2.40.0