slang_pragma_recorder.cpp revision f4ea4fd843acd4eacbc89973fedf8bcf692fe73c
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::PragmaIntroducerKind Introducer,
58                                  clang::Token &FirstToken) {
59  clang::Token &CurrentToken = FirstToken;
60  std::string PragmaName, PragmaValue = "";
61  // Pragma in ACC should be a name/value pair
62
63  if (GetPragmaNameFromToken(FirstToken, PragmaName)) {
64    // start parsing the value '(' PragmaValue ')', if we have one.
65    const clang::Token* NextToken = &PP.LookAhead(0);
66
67    if (NextToken->is(clang::tok::l_paren))
68      PP.LexUnexpandedToken(CurrentToken);
69    else
70      goto end_parsing_pragma_value;
71
72    NextToken = &PP.LookAhead(0);
73    if (GetPragmaValueFromToken(*NextToken, PragmaValue)) {
74      PP.Lex(CurrentToken);
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    }
83
84    if (!NextToken->is(clang::tok::r_paren)) {
85      NextToken = &PP.LookAhead(0);
86      if (NextToken->is(clang::tok::r_paren)) {
87        PP.LexUnexpandedToken(CurrentToken);
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      }
97    }
98  } else {
99    PP.Diag(FirstToken.getLocation(),
100            PP.getDiagnostics().getCustomDiagID(
101                clang::DiagnosticsEngine::Error,
102                "no pragma name or value"));
103    return;
104  }
105
106 end_parsing_pragma_value:
107
108  // PragmaValue may be an empty string.
109  mPragmas->push_back(make_pair(PragmaName, PragmaValue));
110
111  // Inform lex to eat the token
112  PP.LexUnexpandedToken(CurrentToken);
113
114  return;
115}
116
117}  // namespace slang
118