Skip to content

Commit 43c0aa6

Browse files
committed
C++ front-end: parse GCC attributes
1 parent ba0df47 commit 43c0aa6

File tree

3 files changed

+194
-15
lines changed

3 files changed

+194
-15
lines changed

src/ansi-c/scanner.l

+3-8
Original file line numberDiff line numberDiff line change
@@ -939,14 +939,9 @@ __decltype { if(PARSER.cpp98 &&
939939
PARSER.mode==configt::ansi_ct::flavourt::CODEWARRIOR ||
940940
PARSER.mode==configt::ansi_ct::flavourt::ARM)
941941
{
942-
if(PARSER.cpp98)
943-
BEGIN(IGNORE_PARENS);
944-
else
945-
{
946-
BEGIN(GCC_ATTRIBUTE1);
947-
loc();
948-
return TOK_GCC_ATTRIBUTE;
949-
}
942+
BEGIN(GCC_ATTRIBUTE1);
943+
loc();
944+
return TOK_GCC_ATTRIBUTE;
950945
}
951946
else
952947
return make_identifier();

src/cpp/cpp_typecheck_type.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ void cpp_typecheckt::typecheck_type(typet &type)
133133
if(type.subtype().get_bool(ID_C_volatile))
134134
type.set(ID_C_volatile, true);
135135
}
136+
else if(type.id()==ID_vector)
137+
{
138+
typecheck_vector_type(to_vector_type(type));
139+
}
136140
else if(type.id()==ID_code)
137141
{
138142
code_typet &code_type=to_code_type(type);

src/cpp/parse.cpp

+187-7
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ class Parser // NOLINT(readability/identifiers)
276276
bool optStorageSpec(cpp_storage_spect &);
277277
bool optCvQualify(typet &);
278278
bool optAlignas(typet &);
279-
bool rAttribute();
279+
bool rAttribute(typet &);
280280
bool optAttribute(cpp_declarationt &);
281281
bool optIntegralTypeOrClassSpec(typet &);
282282
bool rConstructorDecl(
@@ -473,8 +473,10 @@ void Parser::merge_types(const typet &src, typet &dest)
473473
{
474474
if(dest.id()!=ID_merged_type)
475475
{
476+
source_locationt location=dest.source_location();
476477
typet tmp(ID_merged_type);
477478
tmp.move_to_subtypes(dest);
479+
tmp.add_source_location()=location;
478480
dest=tmp;
479481
}
480482

@@ -2044,7 +2046,7 @@ bool Parser::optCvQualify(typet &cv)
20442046
break;
20452047

20462048
case TOK_GCC_ATTRIBUTE:
2047-
if(!rAttribute())
2049+
if(!rAttribute(cv))
20482050
return false;
20492051
break;
20502052

@@ -2103,28 +2105,204 @@ bool Parser::optAlignas(typet &cv)
21032105
return true;
21042106
}
21052107

2106-
bool Parser::rAttribute()
2108+
bool Parser::rAttribute(typet &t)
21072109
{
2110+
#ifdef DEBUG
2111+
indenter _i;
2112+
std::cout << std::string(__indent, ' ') << "Parser::rAttribute "
2113+
<< lex.LookAhead(0);
2114+
#endif
21082115
cpp_tokent tk;
21092116
lex.get_token(tk);
21102117

21112118
switch(tk.kind)
21122119
{
21132120
case '(':
2114-
rAttribute();
2121+
if(lex.LookAhead(0)!=')')
2122+
rAttribute(t);
2123+
21152124
if(lex.LookAhead(0)!=')')
21162125
return false;
21172126
lex.get_token(tk);
2118-
break;
2127+
return true;
21192128

2120-
case TOK_IDENTIFIER:
2129+
case TOK_GCC_ATTRIBUTE_PACKED:
2130+
{
2131+
typet attr(ID_packed);
2132+
set_location(attr, tk);
2133+
merge_types(attr, t);
2134+
break;
2135+
}
2136+
2137+
case TOK_GCC_ATTRIBUTE_TRANSPARENT_UNION:
2138+
{
2139+
typet attr(ID_transparent_union);
2140+
set_location(attr, tk);
2141+
merge_types(attr, t);
2142+
break;
2143+
}
2144+
2145+
case TOK_GCC_ATTRIBUTE_VECTOR_SIZE:
2146+
{
2147+
cpp_tokent tk2, tk3;
2148+
2149+
if(lex.get_token(tk2)!='(')
2150+
return false;
2151+
2152+
exprt exp;
2153+
if(!rCommaExpression(exp))
2154+
return false;
2155+
2156+
if(lex.get_token(tk3)!=')')
2157+
return false;
2158+
2159+
vector_typet attr(nil_typet(), exp);
2160+
attr.add_source_location()=exp.source_location();
2161+
merge_types(attr, t);
2162+
break;
2163+
}
2164+
2165+
case TOK_GCC_ATTRIBUTE_ALIGNED:
2166+
{
2167+
typet attr(ID_aligned);
2168+
set_location(attr, tk);
2169+
2170+
if(lex.LookAhead(0)=='(')
2171+
{
2172+
cpp_tokent tk2, tk3;
2173+
2174+
if(lex.get_token(tk2)!='(')
2175+
return false;
2176+
2177+
exprt exp;
2178+
if(!rCommaExpression(exp))
2179+
return false;
2180+
2181+
if(lex.get_token(tk3)!=')')
2182+
return false;
2183+
2184+
attr.add(ID_size, exp);
2185+
}
2186+
2187+
merge_types(attr, t);
2188+
break;
2189+
}
2190+
2191+
case TOK_GCC_ATTRIBUTE_MODE:
2192+
{
2193+
cpp_tokent tk2, tk3;
2194+
2195+
if(lex.get_token(tk2)!='(')
2196+
return false;
2197+
2198+
irept name;
2199+
if(!rName(name))
2200+
return false;
2201+
2202+
if(lex.get_token(tk3)!=')')
2203+
return false;
2204+
2205+
typet attr(ID_gcc_attribute_mode);
2206+
set_location(attr, tk);
2207+
attr.set(ID_size, name.get(ID_identifier));
2208+
merge_types(attr, t);
2209+
break;
2210+
}
2211+
2212+
case TOK_GCC_ATTRIBUTE_GNU_INLINE:
2213+
{
2214+
typet attr(ID_static);
2215+
set_location(attr, tk);
2216+
merge_types(attr, t);
2217+
break;
2218+
}
2219+
2220+
case TOK_GCC_ATTRIBUTE_WEAK:
2221+
{
2222+
typet attr(ID_weak);
2223+
set_location(attr, tk);
2224+
merge_types(attr, t);
2225+
break;
2226+
}
2227+
2228+
case TOK_GCC_ATTRIBUTE_ALIAS:
2229+
{
2230+
cpp_tokent tk2, tk3, tk4;
2231+
2232+
if(lex.get_token(tk2)!='(')
2233+
return false;
2234+
2235+
if(!rString(tk3))
2236+
return false;
2237+
2238+
if(lex.get_token(tk4)!=')')
2239+
return false;
2240+
2241+
typet attr(ID_alias);
2242+
set_location(attr, tk);
2243+
attr.move_to_sub(tk3.data);
2244+
merge_types(attr, t);
2245+
break;
2246+
}
2247+
2248+
case TOK_GCC_ATTRIBUTE_SECTION:
2249+
{
2250+
cpp_tokent tk2, tk3, tk4;
2251+
2252+
if(lex.get_token(tk2)!='(')
2253+
return false;
2254+
2255+
if(!rString(tk3))
2256+
return false;
2257+
2258+
if(lex.get_token(tk4)!=')')
2259+
return false;
2260+
2261+
typet attr(ID_section);
2262+
set_location(attr, tk);
2263+
attr.move_to_sub(tk3.data);
2264+
merge_types(attr, t);
2265+
break;
2266+
}
2267+
2268+
case TOK_GCC_ATTRIBUTE_NORETURN:
2269+
{
2270+
typet attr(ID_noreturn);
2271+
set_location(attr, tk);
2272+
merge_types(attr, t);
2273+
break;
2274+
}
2275+
2276+
case TOK_GCC_ATTRIBUTE_CONSTRUCTOR:
2277+
{
2278+
typet attr(ID_constructor);
2279+
set_location(attr, tk);
2280+
merge_types(attr, t);
2281+
break;
2282+
}
2283+
2284+
case TOK_GCC_ATTRIBUTE_DESTRUCTOR:
2285+
{
2286+
typet attr(ID_destructor);
2287+
set_location(attr, tk);
2288+
merge_types(attr, t);
2289+
break;
2290+
}
2291+
2292+
case ',':
2293+
if(lex.LookAhead(0)==')')
2294+
// the scanner ignored an attribute
2295+
return true;
21212296
break;
21222297

21232298
default:
21242299
return false;
21252300
}
21262301

2127-
return true;
2302+
if(lex.LookAhead(0)==')')
2303+
return true;
2304+
2305+
return rAttribute(t);
21282306
}
21292307

21302308
bool Parser::optAttribute(cpp_declarationt &declaration)
@@ -2971,6 +3149,8 @@ bool Parser::rDeclarator(
29713149
break;
29723150
}
29733151

3152+
optCvQualify(d_outer);
3153+
29743154
#ifdef DEBUG
29753155
std::cout << std::string(__indent, ' ') << "Parser::rDeclarator2 13\n";
29763156
#endif

0 commit comments

Comments
 (0)