zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 1 | #include "slang_pragma_recorder.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 2 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 3 | #include "clang/Basic/TokenKinds.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 4 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 5 | #include "clang/Lex/Preprocessor.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 6 | #include "clang/Lex/Token.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 7 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 8 | using namespace slang; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 9 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 10 | bool PragmaRecorder::GetPragmaNameFromToken(const clang::Token &Token, |
| 11 | std::string &PragmaName) { |
| 12 | if (Token.isLiteral()) |
| 13 | PragmaName.assign(Token.getLiteralData(), Token.getLength()); |
| 14 | else if (Token.is(clang::tok::identifier)) |
| 15 | PragmaName.assign(Token.getIdentifierInfo()->getNameStart(), |
| 16 | Token.getIdentifierInfo()->getLength()); |
| 17 | else |
| 18 | return false; |
| 19 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 20 | } |
| 21 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 22 | bool PragmaRecorder::GetPragmaValueFromToken(const clang::Token &Token, |
| 23 | std::string &PragmaValue) { |
| 24 | // Same as the GetPragmaName() |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 25 | if (Token.is(clang::tok::r_paren)) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 26 | PragmaValue.clear(); |
| 27 | else |
| 28 | return GetPragmaNameFromToken(Token, PragmaValue); |
| 29 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 30 | } |
| 31 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 32 | PragmaRecorder::PragmaRecorder(PragmaList &Pragmas) |
| 33 | : PragmaHandler(), |
| 34 | mPragmas(Pragmas) { |
Shih-wei Liao | 1ebc0ca | 2010-09-14 10:57:21 -0700 | [diff] [blame] | 35 | return; |
| 36 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 37 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 38 | void PragmaRecorder::HandlePragma(clang::Preprocessor &PP, |
| 39 | clang::Token &FirstToken) { |
| 40 | clang::Token &CurrentToken = FirstToken; |
| 41 | std::string PragmaName, PragmaValue = ""; |
| 42 | // Pragma in ACC should be a name/value pair |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 43 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 44 | if (GetPragmaNameFromToken(FirstToken, PragmaName)) { |
| 45 | // start parsing the value '(' PragmaValue ')' |
| 46 | const clang::Token* NextToken = &PP.LookAhead(0); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 47 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 48 | if (NextToken->is(clang::tok::l_paren)) |
| 49 | PP.LexUnexpandedToken(CurrentToken); |
| 50 | else |
| 51 | goto end_parsing_pragma_value; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 52 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 53 | NextToken = &PP.LookAhead(0); |
| 54 | if (GetPragmaValueFromToken(*NextToken, PragmaValue)) |
| 55 | PP.Lex(CurrentToken); |
| 56 | else |
| 57 | goto end_parsing_pragma_value; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 58 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 59 | if (!NextToken->is(clang::tok::r_paren)) { |
| 60 | NextToken = &PP.LookAhead(0); |
| 61 | if (NextToken->is(clang::tok::r_paren)) |
| 62 | PP.LexUnexpandedToken(CurrentToken); |
| 63 | else |
| 64 | goto end_parsing_pragma_value; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 67 | // Until now, we ensure that we have a pragma name/value pair |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 68 | mPragmas.push_back(make_pair(PragmaName, PragmaValue)); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 69 | } |
| 70 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 71 | end_parsing_pragma_value: |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 72 | // Infor lex to eat the token |
| 73 | PP.LexUnexpandedToken(CurrentToken); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 74 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 75 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 76 | } |