Skip to content

Commit

Permalink
feat: Add support for bitwise attribute for strong typedefs.
Browse files Browse the repository at this point in the history
The `sparse` tool can validate these.
  • Loading branch information
iphydf committed Feb 3, 2024
1 parent 61daedb commit c5e6d93
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Language/Cimple/Ast.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ data NodeF lexeme a
| Struct lexeme [a]
| Union lexeme [a]
| MemberDecl a (Maybe lexeme)
| TyBitwise a
| TyForce a
| TyConst a
| TyPointer a
| TyStruct lexeme
Expand Down
4 changes: 2 additions & 2 deletions src/Language/Cimple/DescribeAst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ describeExpected options
| wants ["break", "const", "continue", "ID_CONST", "VLA"] = "statement or declaration"
| wants ["ID_FUNC_TYPE", "non_null", "static", "'#include'"] = "top-level declaration or definition"
| options == ["ID_STD_TYPE", "ID_SUE_TYPE", "struct", "void"] = "type specifier"
| options == ["ID_STD_TYPE", "ID_SUE_TYPE", "const", "struct", "void"] = "type specifier"
| options == ["ID_STD_TYPE", "ID_SUE_TYPE", "bitwise", "const", "force", "struct", "void"] = "type specifier"
| options == ["ID_CONST", "ID_VAR", "LIT_CHAR", "LIT_FALSE", "LIT_INTEGER", "'{'"] = "constant or literal"
| ["ID_FUNC_TYPE", "ID_STD_TYPE", "ID_SUE_TYPE", "ID_VAR"] `isPrefixOf` options = "type specifier or variable name"
| ["ID_FUNC_TYPE", "ID_STD_TYPE", "ID_SUE_TYPE", "const"] `isPrefixOf` options = "type specifier"
| ["ID_FUNC_TYPE", "ID_STD_TYPE", "ID_SUE_TYPE", "bitwise", "const"] `isPrefixOf` options = "type specifier"
| ["ID_CONST", "sizeof", "LIT_CHAR", "LIT_FALSE", "LIT_TRUE", "LIT_INTEGER"] `isPrefixOf` options = "constant expression"
| ["ID_CONST", "ID_SUE_TYPE", "'/*'"] `isPrefixOf` options = "enumerator, type name, or comment"
| wants ["'defined'"] = "preprocessor constant expression"
Expand Down
2 changes: 2 additions & 0 deletions src/Language/Cimple/Lexer.x
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ tokens :-
<0> "#define" { mkL PpDefine `andBegin` ppSC }
<0> "#undef" { mkL PpUndef }
<0> "#include" { mkL PpInclude }
<0,ppSC> "bitwise" { mkL KwBitwise }
<0,ppSC> "break" { mkL KwBreak }
<0,ppSC> "case" { mkL KwCase }
<0,ppSC> "const" { mkL KwConst }
Expand All @@ -161,6 +162,7 @@ tokens :-
<0,ppSC> "enum" { mkL KwEnum }
<0,ppSC> "extern" { mkL KwExtern }
<0,ppSC> "for" { mkL KwFor }
<0,ppSC> "force" { mkL KwForce }
<0,ppSC> "goto" { mkL KwGoto }
<0,ppSC> "if" { mkL KwIf }
<0,ppSC> "non_null" { mkL KwNonNull }
Expand Down
4 changes: 4 additions & 0 deletions src/Language/Cimple/MapAst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ instance MapAst itext otext (Node (Lexeme itext)) where
Fix <$> (Union <$> recurse name <*> recurse members)
MemberDecl decl bits ->
Fix <$> (MemberDecl <$> recurse decl <*> recurse bits)
TyBitwise ty ->
Fix <$> (TyBitwise <$> recurse ty)
TyForce ty ->
Fix <$> (TyForce <$> recurse ty)
TyConst ty ->
Fix <$> (TyConst <$> recurse ty)
TyPointer ty ->
Expand Down
6 changes: 5 additions & 1 deletion src/Language/Cimple/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import Language.Cimple.Tokens (LexemeClass (..))
ID_STD_TYPE { L _ IdStdType _ }
ID_SUE_TYPE { L _ IdSueType _ }
ID_VAR { L _ IdVar _ }
bitwise { L _ KwBitwise _ }
break { L _ KwBreak _ }
case { L _ KwCase _ }
const { L _ KwConst _ }
Expand All @@ -53,6 +54,7 @@ import Language.Cimple.Tokens (LexemeClass (..))
enum { L _ KwEnum _ }
extern { L _ KwExtern _ }
for { L _ KwFor _ }
force { L _ KwForce _ }
GNU_PRINTF { L _ KwGnuPrintf _ }
goto { L _ KwGoto _ }
if { L _ KwIf _ }
Expand Down Expand Up @@ -666,7 +668,9 @@ TypedefDecl

QualType :: { NonTerm }
QualType
: LeafType { $1 }
: bitwise ID_STD_TYPE { Fix (TyBitwise (Fix (TyStd $2))) }
| force LeafType { Fix (TyForce $2) }
| LeafType { $1 }
| LeafType '*' { tyPointer $1 }
| LeafType '*' '*' { tyPointer (tyPointer $1) }
| LeafType '*' '*' const { tyConst (tyPointer (tyPointer $1)) }
Expand Down
4 changes: 4 additions & 0 deletions src/Language/Cimple/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import Text.PrettyPrint.ANSI.Leijen
indentWidth :: Int
indentWidth = 2

kwBitwise = dullgreen $ text "bitwise"

Check warning on line 30 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘dullgreen’

Check warning on line 30 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘text’ (imported from Text.PrettyPrint.ANSI.Leijen):

Check warning on line 30 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘dullgreen’

Check warning on line 30 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘text’ (imported from Text.PrettyPrint.ANSI.Leijen):
kwBreak = dullred $ text "break"

Check warning on line 31 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘dullred’

Check warning on line 31 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘text’ (imported from Text.PrettyPrint.ANSI.Leijen):

Check warning on line 31 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘dullred’

Check warning on line 31 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘text’ (imported from Text.PrettyPrint.ANSI.Leijen):
kwCase = dullred $ text "case"

Check warning on line 32 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘dullred’

Check warning on line 32 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘text’ (imported from Text.PrettyPrint.ANSI.Leijen):

Check warning on line 32 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘dullred’

Check warning on line 32 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘text’ (imported from Text.PrettyPrint.ANSI.Leijen):
kwConst = dullgreen $ text "const"

Check warning on line 33 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘dullgreen’

Check warning on line 33 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘text’ (imported from Text.PrettyPrint.ANSI.Leijen):

Check warning on line 33 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘dullgreen’

Check warning on line 33 in src/Language/Cimple/Pretty.hs

View workflow job for this annotation

GitHub Actions / publish / Publish to Hackage

In the use of ‘text’ (imported from Text.PrettyPrint.ANSI.Leijen):
Expand All @@ -37,6 +38,7 @@ kwElse = dullred $ text "else"
kwEnum = dullgreen $ text "enum"
kwExtern = dullgreen $ text "extern"
kwFor = dullred $ text "for"
kwForce = dullgreen $ text "force"
kwGnuPrintf = dullgreen $ text "GNU_PRINTF"
kwGoto = dullred $ text "goto"
kwIf = dullred $ text "if"
Expand Down Expand Up @@ -402,6 +404,8 @@ ppNode = foldFix go
DeclSpecArray Nothing -> text "[]"
DeclSpecArray (Just dim) -> brackets dim

TyBitwise ty -> kwBitwise <+> ty
TyForce ty -> kwForce <+> ty
TyPointer ty -> ty <> char '*'
TyConst ty -> ty <+> kwConst
TyUserDefined l -> dullgreen $ ppLexeme l
Expand Down
2 changes: 2 additions & 0 deletions src/Language/Cimple/Tokens.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ data LexemeClass
| IdStdType
| IdSueType
| IdVar
| KwBitwise
| KwBreak
| KwCase
| KwConst
Expand All @@ -23,6 +24,7 @@ data LexemeClass
| KwEnum
| KwExtern
| KwFor
| KwForce
| KwGnuPrintf
| KwGoto
| KwIf
Expand Down
4 changes: 4 additions & 0 deletions src/Language/Cimple/TraverseAst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,10 @@ instance TraverseAst text (Node (Lexeme text)) where
_ <- recurse decl
_ <- recurse bits
pure ()
TyBitwise ty ->
recurse ty
TyForce ty ->
recurse ty
TyConst ty ->
recurse ty
TyPointer ty ->
Expand Down
2 changes: 2 additions & 0 deletions test/Language/CimpleSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ sampleToken c = case c of
IdStdType -> "uint32_t"
IdSueType -> "Sue_Type"
IdVar -> "var"
KwBitwise -> "bitwise"
KwBreak -> "break"
KwCase -> "case"
KwConst -> "const"
Expand All @@ -34,6 +35,7 @@ sampleToken c = case c of
KwEnum -> "enum"
KwExtern -> "extern"
KwFor -> "for"
KwForce -> "force"
KwGnuPrintf -> "gnu_printf"
KwGoto -> "goto"
KwIf -> "if"
Expand Down

0 comments on commit c5e6d93

Please sign in to comment.