slang_pragma_recorder.cpp revision e639eb5caa2c386b4a60659a4929e8a6141a2cbe
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
17#include "slang_pragma_recorder.h"
18
19#include <string>
20
21#include "clang/Basic/TokenKinds.h"
22
23#include "clang/Lex/Preprocessor.h"
24#include "clang/Lex/Token.h"
25
26namespace slang {
27
28bool 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;
38}
39
40bool PragmaRecorder::GetPragmaValueFromToken(const clang::Token &Token,
41                                             std::string &PragmaValue) {
42  // Same as the GetPragmaName()
43  if (Token.is(clang::tok::r_paren))
44    PragmaValue.clear();
45  else
46    return GetPragmaNameFromToken(Token, PragmaValue);
47  return true;
48}
49
50PragmaRecorder::PragmaRecorder(PragmaList &Pragmas)
51    : PragmaHandler(),
52      mPragmas(Pragmas) {
53  return;
54}
55
56void PragmaRecorder::HandlePragma(clang::Preprocessor &PP,
57                                  clang::Token &FirstToken) {
58  clang::Token &CurrentToken = FirstToken;
59  std::string PragmaName, PragmaValue = "";
60  // Pragma in ACC should be a name/value pair
61
62  if (GetPragmaNameFromToken(FirstToken, PragmaName)) {
63    // start parsing the value '(' PragmaValue ')'
64    const clang::Token* NextToken = &PP.LookAhead(0);
65
66    if (NextToken->is(clang::tok::l_paren))
67      PP.LexUnexpandedToken(CurrentToken);
68    else
69      goto end_parsing_pragma_value;
70
71    NextToken = &PP.LookAhead(0);
72    if (GetPragmaValueFromToken(*NextToken, PragmaValue))
73      PP.Lex(CurrentToken);
74    else
75      goto end_parsing_pragma_value;
76
77    if (!NextToken->is(clang::tok::r_paren)) {
78      NextToken = &PP.LookAhead(0);
79      if (NextToken->is(clang::tok::r_paren))
80        PP.LexUnexpandedToken(CurrentToken);
81      else
82        goto end_parsing_pragma_value;
83    }
84
85    // Until now, we ensure that we have a pragma name/value pair
86    mPragmas.push_back(make_pair(PragmaName, PragmaValue));
87  }
88
89 end_parsing_pragma_value:
90  // Infor lex to eat the token
91  PP.LexUnexpandedToken(CurrentToken);
92
93  return;
94}
95
96}  // namespace slang
97