Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 17 | #include "slang_pragma_recorder.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 18 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 19 | #include <string> |
| 20 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 21 | #include "clang/Basic/TokenKinds.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 22 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 23 | #include "clang/Lex/Preprocessor.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 24 | #include "clang/Lex/Token.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 25 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 26 | namespace slang { |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 27 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 28 | bool PragmaRecorder::GetPragmaNameFromToken(const clang::Token &Token, |
| 29 | std::string &PragmaName) { |
| 30 | if (Token.isLiteral()) |
| 31 | PragmaName.assign(Token.getLiteralData(), Token.getLength()); |
| 32 | else if (Token.is(clang::tok::identifier)) |
| 33 | PragmaName.assign(Token.getIdentifierInfo()->getNameStart(), |
| 34 | Token.getIdentifierInfo()->getLength()); |
| 35 | else |
| 36 | return false; |
| 37 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 40 | bool PragmaRecorder::GetPragmaValueFromToken(const clang::Token &Token, |
| 41 | std::string &PragmaValue) { |
| 42 | // Same as the GetPragmaName() |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 43 | if (Token.is(clang::tok::r_paren)) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 44 | PragmaValue.clear(); |
| 45 | else |
| 46 | return GetPragmaNameFromToken(Token, PragmaValue); |
| 47 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 50 | PragmaRecorder::PragmaRecorder(PragmaList *Pragmas) |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 51 | : PragmaHandler(), |
| 52 | mPragmas(Pragmas) { |
Shih-wei Liao | 1ebc0ca | 2010-09-14 10:57:21 -0700 | [diff] [blame] | 53 | return; |
| 54 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 55 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 56 | void PragmaRecorder::HandlePragma(clang::Preprocessor &PP, |
Shih-wei Liao | df5bcce | 2011-02-28 18:39:23 -0800 | [diff] [blame] | 57 | clang::PragmaIntroducerKind Introducer, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 58 | clang::Token &FirstToken) { |
| 59 | clang::Token &CurrentToken = FirstToken; |
| 60 | std::string PragmaName, PragmaValue = ""; |
| 61 | // Pragma in ACC should be a name/value pair |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 62 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 63 | if (GetPragmaNameFromToken(FirstToken, PragmaName)) { |
Stephen Hines | f4ea4fd | 2012-03-27 08:41:11 -0700 | [diff] [blame] | 64 | // start parsing the value '(' PragmaValue ')', if we have one. |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 65 | const clang::Token* NextToken = &PP.LookAhead(0); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 66 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 67 | if (NextToken->is(clang::tok::l_paren)) |
| 68 | PP.LexUnexpandedToken(CurrentToken); |
| 69 | else |
| 70 | goto end_parsing_pragma_value; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 71 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 72 | NextToken = &PP.LookAhead(0); |
Stephen Hines | f4ea4fd | 2012-03-27 08:41:11 -0700 | [diff] [blame] | 73 | if (GetPragmaValueFromToken(*NextToken, PragmaValue)) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 74 | PP.Lex(CurrentToken); |
Stephen Hines | f4ea4fd | 2012-03-27 08:41:11 -0700 | [diff] [blame] | 75 | } else { |
| 76 | PP.LexUnexpandedToken(CurrentToken); |
| 77 | PP.Diag(NextToken->getLocation(), |
| 78 | PP.getDiagnostics().getCustomDiagID( |
| 79 | clang::DiagnosticsEngine::Error, |
| 80 | "expected value after '#pragma %0('")) << PragmaName; |
| 81 | return; |
| 82 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 83 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 84 | if (!NextToken->is(clang::tok::r_paren)) { |
| 85 | NextToken = &PP.LookAhead(0); |
Stephen Hines | f4ea4fd | 2012-03-27 08:41:11 -0700 | [diff] [blame] | 86 | if (NextToken->is(clang::tok::r_paren)) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 87 | PP.LexUnexpandedToken(CurrentToken); |
Stephen Hines | f4ea4fd | 2012-03-27 08:41:11 -0700 | [diff] [blame] | 88 | } else { |
| 89 | PP.LexUnexpandedToken(CurrentToken); |
| 90 | PP.Diag(NextToken->getLocation(), |
| 91 | PP.getDiagnostics().getCustomDiagID( |
| 92 | clang::DiagnosticsEngine::Error, |
| 93 | "missing ')' after '#pragma %0(%1'")) |
| 94 | << PragmaName << PragmaValue; |
| 95 | return; |
| 96 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 97 | } |
Stephen Hines | f4ea4fd | 2012-03-27 08:41:11 -0700 | [diff] [blame] | 98 | } else { |
| 99 | PP.Diag(FirstToken.getLocation(), |
| 100 | PP.getDiagnostics().getCustomDiagID( |
| 101 | clang::DiagnosticsEngine::Error, |
| 102 | "no pragma name or value")); |
| 103 | return; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 104 | } |
| 105 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 106 | end_parsing_pragma_value: |
Stephen Hines | f4ea4fd | 2012-03-27 08:41:11 -0700 | [diff] [blame] | 107 | |
| 108 | // PragmaValue may be an empty string. |
| 109 | mPragmas->push_back(make_pair(PragmaName, PragmaValue)); |
| 110 | |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 111 | // Inform lex to eat the token |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 112 | PP.LexUnexpandedToken(CurrentToken); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 113 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 114 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 115 | } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 116 | |
| 117 | } // namespace slang |