2008-10-04 23:21:03 +04:00
|
|
|
//===--- ParsePragma.cpp - Language specific pragma parsing ---------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the language specific #pragma handlers.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ParsePragma.h"
|
2009-01-29 08:15:15 +03:00
|
|
|
#include "clang/Parse/ParseDiagnostic.h"
|
2008-10-04 23:21:03 +04:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Parse/Action.h"
|
2009-03-24 01:28:25 +03:00
|
|
|
#include "clang/Parse/Parser.h"
|
2008-10-04 23:21:03 +04:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
// #pragma pack(...) comes in the following delicious flavors:
|
|
|
|
// pack '(' [integer] ')'
|
|
|
|
// pack '(' 'show' ')'
|
|
|
|
// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
|
|
|
|
void PragmaPackHandler::HandlePragma(Preprocessor &PP, Token &PackTok) {
|
|
|
|
SourceLocation PackLoc = PackTok.getLocation();
|
|
|
|
|
|
|
|
Token Tok;
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2009-03-24 01:28:25 +03:00
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
|
2008-10-04 23:21:03 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Action::PragmaPackKind Kind = Action::PPK_Default;
|
|
|
|
IdentifierInfo *Name = 0;
|
2008-12-09 23:22:58 +03:00
|
|
|
Action::OwningExprResult Alignment(Actions);
|
2008-10-04 23:21:03 +04:00
|
|
|
SourceLocation LParenLoc = Tok.getLocation();
|
2009-09-09 19:08:12 +04:00
|
|
|
PP.Lex(Tok);
|
2008-10-04 23:21:03 +04:00
|
|
|
if (Tok.is(tok::numeric_constant)) {
|
|
|
|
Alignment = Actions.ActOnNumericConstant(Tok);
|
2008-12-09 16:15:23 +03:00
|
|
|
if (Alignment.isInvalid())
|
2008-10-04 23:21:03 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
PP.Lex(Tok);
|
|
|
|
} else if (Tok.is(tok::identifier)) {
|
|
|
|
const IdentifierInfo *II = Tok.getIdentifierInfo();
|
2008-11-24 00:45:46 +03:00
|
|
|
if (II->isStr("show")) {
|
2008-10-04 23:21:03 +04:00
|
|
|
Kind = Action::PPK_Show;
|
|
|
|
PP.Lex(Tok);
|
|
|
|
} else {
|
2008-11-24 00:45:46 +03:00
|
|
|
if (II->isStr("push")) {
|
2008-10-04 23:21:03 +04:00
|
|
|
Kind = Action::PPK_Push;
|
2008-11-24 00:45:46 +03:00
|
|
|
} else if (II->isStr("pop")) {
|
2008-10-04 23:21:03 +04:00
|
|
|
Kind = Action::PPK_Pop;
|
|
|
|
} else {
|
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_invalid_action);
|
|
|
|
return;
|
2009-09-09 19:08:12 +04:00
|
|
|
}
|
2008-10-04 23:21:03 +04:00
|
|
|
PP.Lex(Tok);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-10-04 23:21:03 +04:00
|
|
|
if (Tok.is(tok::comma)) {
|
|
|
|
PP.Lex(Tok);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-10-04 23:21:03 +04:00
|
|
|
if (Tok.is(tok::numeric_constant)) {
|
|
|
|
Alignment = Actions.ActOnNumericConstant(Tok);
|
2008-12-09 16:15:23 +03:00
|
|
|
if (Alignment.isInvalid())
|
2008-10-04 23:21:03 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
PP.Lex(Tok);
|
|
|
|
} else if (Tok.is(tok::identifier)) {
|
|
|
|
Name = Tok.getIdentifierInfo();
|
|
|
|
PP.Lex(Tok);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-10-04 23:21:03 +04:00
|
|
|
if (Tok.is(tok::comma)) {
|
|
|
|
PP.Lex(Tok);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-10-04 23:21:03 +04:00
|
|
|
if (Tok.isNot(tok::numeric_constant)) {
|
2008-11-24 00:45:46 +03:00
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
|
2008-10-04 23:21:03 +04:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-10-04 23:21:03 +04:00
|
|
|
Alignment = Actions.ActOnNumericConstant(Tok);
|
2008-12-09 16:15:23 +03:00
|
|
|
if (Alignment.isInvalid())
|
2008-10-04 23:21:03 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
PP.Lex(Tok);
|
|
|
|
}
|
|
|
|
} else {
|
2008-11-24 00:45:46 +03:00
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
|
2008-10-04 23:21:03 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-09 16:15:23 +03:00
|
|
|
}
|
2008-10-04 23:21:03 +04:00
|
|
|
|
|
|
|
if (Tok.isNot(tok::r_paren)) {
|
2009-03-24 01:28:25 +03:00
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
|
2008-10-04 23:21:03 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-27 03:29:06 +04:00
|
|
|
SourceLocation RParenLoc = Tok.getLocation();
|
2009-06-05 04:49:58 +04:00
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::eom)) {
|
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-10 03:02:53 +03:00
|
|
|
Actions.ActOnPragmaPack(Kind, Name, Alignment.release(), PackLoc,
|
2008-10-04 23:21:03 +04:00
|
|
|
LParenLoc, RParenLoc);
|
|
|
|
}
|
|
|
|
|
2010-07-31 23:17:07 +04:00
|
|
|
// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
|
|
|
|
// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
|
|
|
|
static void ParseAlignPragma(Action &Actions, Preprocessor &PP, Token &FirstTok,
|
|
|
|
bool IsOptions) {
|
2010-05-27 03:29:06 +04:00
|
|
|
Token Tok;
|
2010-07-31 23:17:07 +04:00
|
|
|
|
|
|
|
if (IsOptions) {
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::identifier) ||
|
|
|
|
!Tok.getIdentifierInfo()->isStr("align")) {
|
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
|
|
|
|
return;
|
|
|
|
}
|
2010-05-27 03:29:06 +04:00
|
|
|
}
|
2010-05-27 22:42:09 +04:00
|
|
|
|
2010-05-27 03:29:06 +04:00
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::equal)) {
|
2010-07-31 23:17:07 +04:00
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
|
|
|
|
<< IsOptions;
|
2010-05-27 03:29:06 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::identifier)) {
|
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
|
2010-07-31 23:17:07 +04:00
|
|
|
<< (IsOptions ? "options" : "align");
|
2010-05-27 03:29:06 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Action::PragmaOptionsAlignKind Kind = Action::POAK_Natural;
|
|
|
|
const IdentifierInfo *II = Tok.getIdentifierInfo();
|
2010-05-27 22:42:09 +04:00
|
|
|
if (II->isStr("native"))
|
|
|
|
Kind = Action::POAK_Native;
|
|
|
|
else if (II->isStr("natural"))
|
2010-05-27 03:29:06 +04:00
|
|
|
Kind = Action::POAK_Natural;
|
2010-05-27 22:42:17 +04:00
|
|
|
else if (II->isStr("packed"))
|
|
|
|
Kind = Action::POAK_Packed;
|
2010-05-27 03:29:06 +04:00
|
|
|
else if (II->isStr("power"))
|
|
|
|
Kind = Action::POAK_Power;
|
|
|
|
else if (II->isStr("mac68k"))
|
|
|
|
Kind = Action::POAK_Mac68k;
|
|
|
|
else if (II->isStr("reset"))
|
|
|
|
Kind = Action::POAK_Reset;
|
|
|
|
else {
|
2010-07-31 23:17:07 +04:00
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
|
|
|
|
<< IsOptions;
|
2010-05-27 03:29:06 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceLocation KindLoc = Tok.getLocation();
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::eom)) {
|
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
|
2010-07-31 23:17:07 +04:00
|
|
|
<< (IsOptions ? "options" : "align");
|
2010-05-27 03:29:06 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-31 23:17:07 +04:00
|
|
|
Actions.ActOnPragmaOptionsAlign(Kind, FirstTok.getLocation(), KindLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PragmaAlignHandler::HandlePragma(Preprocessor &PP, Token &AlignTok) {
|
|
|
|
ParseAlignPragma(Actions, PP, AlignTok, /*IsOptions=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PragmaOptionsHandler::HandlePragma(Preprocessor &PP, Token &OptionsTok) {
|
|
|
|
ParseAlignPragma(Actions, PP, OptionsTok, /*IsOptions=*/true);
|
2010-05-27 03:29:06 +04:00
|
|
|
}
|
|
|
|
|
2009-03-24 01:28:25 +03:00
|
|
|
// #pragma unused(identifier)
|
|
|
|
void PragmaUnusedHandler::HandlePragma(Preprocessor &PP, Token &UnusedTok) {
|
|
|
|
// FIXME: Should we be expanding macros here? My guess is no.
|
|
|
|
SourceLocation UnusedLoc = UnusedTok.getLocation();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-24 01:28:25 +03:00
|
|
|
// Lex the left '('.
|
|
|
|
Token Tok;
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SourceLocation LParenLoc = Tok.getLocation();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-24 01:28:25 +03:00
|
|
|
// Lex the declaration reference(s).
|
2009-08-04 03:24:57 +04:00
|
|
|
llvm::SmallVector<Token, 5> Identifiers;
|
2009-03-24 01:28:25 +03:00
|
|
|
SourceLocation RParenLoc;
|
|
|
|
bool LexID = true;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-24 01:28:25 +03:00
|
|
|
while (true) {
|
|
|
|
PP.Lex(Tok);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-24 01:28:25 +03:00
|
|
|
if (LexID) {
|
2009-09-09 19:08:12 +04:00
|
|
|
if (Tok.is(tok::identifier)) {
|
2009-08-04 03:24:57 +04:00
|
|
|
Identifiers.push_back(Tok);
|
2009-03-24 01:28:25 +03:00
|
|
|
LexID = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-08-04 03:24:57 +04:00
|
|
|
// Illegal token!
|
2009-03-24 01:28:25 +03:00
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-24 01:28:25 +03:00
|
|
|
// We are execting a ')' or a ','.
|
|
|
|
if (Tok.is(tok::comma)) {
|
|
|
|
LexID = true;
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-03-24 01:28:25 +03:00
|
|
|
if (Tok.is(tok::r_paren)) {
|
|
|
|
RParenLoc = Tok.getLocation();
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-04 03:24:57 +04:00
|
|
|
// Illegal token!
|
2009-03-24 01:28:25 +03:00
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_punc);
|
|
|
|
return;
|
|
|
|
}
|
2009-06-05 04:49:58 +04:00
|
|
|
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::eom)) {
|
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
|
|
|
|
"unused";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-24 01:28:25 +03:00
|
|
|
// Verify that we have a location for the right parenthesis.
|
|
|
|
assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
|
2009-08-04 03:24:57 +04:00
|
|
|
assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
|
2009-03-24 01:28:25 +03:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
// Perform the action to handle the pragma.
|
2009-08-04 03:24:57 +04:00
|
|
|
Actions.ActOnPragmaUnused(Identifiers.data(), Identifiers.size(),
|
2010-07-02 21:43:08 +04:00
|
|
|
parser.getCurScope(), UnusedLoc, LParenLoc, RParenLoc);
|
2009-03-24 01:28:25 +03:00
|
|
|
}
|
2009-06-05 04:49:58 +04:00
|
|
|
|
|
|
|
// #pragma weak identifier
|
|
|
|
// #pragma weak identifier '=' identifier
|
|
|
|
void PragmaWeakHandler::HandlePragma(Preprocessor &PP, Token &WeakTok) {
|
|
|
|
// FIXME: Should we be expanding macros here? My guess is no.
|
|
|
|
SourceLocation WeakLoc = WeakTok.getLocation();
|
|
|
|
|
|
|
|
Token Tok;
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::identifier)) {
|
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IdentifierInfo *WeakName = Tok.getIdentifierInfo(), *AliasName = 0;
|
|
|
|
SourceLocation WeakNameLoc = Tok.getLocation(), AliasNameLoc;
|
|
|
|
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.is(tok::equal)) {
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::identifier)) {
|
2009-09-09 19:08:12 +04:00
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
|
2009-06-05 04:49:58 +04:00
|
|
|
<< "weak";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
AliasName = Tok.getIdentifierInfo();
|
|
|
|
AliasNameLoc = Tok.getLocation();
|
|
|
|
PP.Lex(Tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::eom)) {
|
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AliasName) {
|
|
|
|
Actions.ActOnPragmaWeakAlias(WeakName, AliasName, WeakLoc, WeakNameLoc,
|
|
|
|
AliasNameLoc);
|
|
|
|
} else {
|
|
|
|
Actions.ActOnPragmaWeakID(WeakName, WeakLoc, WeakNameLoc);
|
|
|
|
}
|
|
|
|
}
|