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 
26 namespace slang {
27 
GetPragmaNameFromToken(const clang::Token & Token,std::string & PragmaName)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;
38 }
39 
GetPragmaValueFromToken(const clang::Token & Token,std::string & PragmaValue)40 bool 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 
PragmaRecorder(PragmaList * Pragmas)50 PragmaRecorder::PragmaRecorder(PragmaList *Pragmas)
51     : PragmaHandler(),
52       mPragmas(Pragmas) {
53 }
54 
HandlePragma(clang::Preprocessor & PP,clang::PragmaIntroducerKind Introducer,clang::Token & FirstToken)55 void PragmaRecorder::HandlePragma(clang::Preprocessor &PP,
56                                   clang::PragmaIntroducerKind Introducer,
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 ')', if we have one.
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       PP.LexUnexpandedToken(CurrentToken);
76       PP.Diag(NextToken->getLocation(),
77               PP.getDiagnostics().getCustomDiagID(
78                   clang::DiagnosticsEngine::Error,
79                   "expected value after '#pragma %0('")) << PragmaName;
80       return;
81     }
82 
83     if (!NextToken->is(clang::tok::r_paren)) {
84       NextToken = &PP.LookAhead(0);
85       if (NextToken->is(clang::tok::r_paren)) {
86         PP.LexUnexpandedToken(CurrentToken);
87       } else {
88         PP.LexUnexpandedToken(CurrentToken);
89         PP.Diag(NextToken->getLocation(),
90                 PP.getDiagnostics().getCustomDiagID(
91                     clang::DiagnosticsEngine::Error,
92                     "missing ')' after '#pragma %0(%1'"))
93             << PragmaName << PragmaValue;
94         return;
95       }
96     }
97   } else {
98     PP.Diag(FirstToken.getLocation(),
99             PP.getDiagnostics().getCustomDiagID(
100                 clang::DiagnosticsEngine::Error,
101                 "no pragma name or value"));
102     return;
103   }
104 
105  end_parsing_pragma_value:
106 
107   // PragmaValue may be an empty string.
108   mPragmas->push_back(make_pair(PragmaName, PragmaValue));
109 
110   // Inform lex to eat the token
111   PP.LexUnexpandedToken(CurrentToken);
112 }
113 
114 }  // namespace slang
115