Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add preliminary support for owner pointer qualifier. #253

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Tokstyle/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ isPointer x = case unFix x of
VarDecl ty _ [] -> isPointer ty
VarDecl{} -> True
TyConst ty -> isPointer ty
TyOwner ty -> isPointer ty
TyPointer{} -> True
TyStd{} -> False
TyStruct{} -> False
Expand Down
3 changes: 3 additions & 0 deletions src/Tokstyle/Linter/Callgraph.hs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ analyse = reverse . flip State.execState [] . linter . (builtins <>) . callgraph

, "WORDS_BIGENDIAN"

-- cake
, "static_set"

, "crypto_aead_xchacha20poly1305_ietf_decrypt"
, "crypto_aead_xchacha20poly1305_ietf_encrypt"
, "crypto_auth_BYTES"
Expand Down
1 change: 1 addition & 0 deletions src/Tokstyle/Linter/CallocType.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ checkTypes funName file castTy sizeofTy = case unFix castTy of
"`" <> funName <> "` should not be used for `" <> showNode castTy
<> "`; use `mem_balloc` instead"
TyPointer ty1 | ty1 `semEq` sizeofTy -> return ()
TyOwner (Fix (TyPointer ty1)) | ty1 `semEq` sizeofTy -> return ()
_ -> warn file castTy $
"`" <> funName <> "` result is cast to `" <> showNode castTy
<> "` but allocated type is `" <> showNode sizeofTy <> "`"
Expand Down
15 changes: 10 additions & 5 deletions src/Tokstyle/Linter/MallocType.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ isByteSize ty = case unFix ty of
TyStd (L _ _ "uint8_t") -> True
_ -> False

removeOwner :: Node (Lexeme Text) -> Node (Lexeme Text)
removeOwner (Fix (TyOwner ty)) = ty
removeOwner ty = ty

checkType :: FilePath -> Text -> Node (Lexeme Text) -> State [Text] ()
checkType file malloc castTy = case unFix castTy of
TyPointer (Fix (TyStd (L _ _ tyName))) | tyName `elem` supportedTypes -> return ()
Expand Down Expand Up @@ -74,14 +78,15 @@ linter = astActions
{ doNode = \file node act ->
case unFix node of
-- Windows API weirdness: ignore completely.
CastExpr (Fix (TyPointer (Fix (TyStd (L _ _ "IP_ADAPTER_INFO"))))) _ -> return ()
CastExpr (Fix (TyPointer (Fix (TyStd (L _ _ "IP_ADAPTER_INFO"))))) _ -> return ()
CastExpr (Fix (TyOwner (Fix (TyPointer (Fix (TyStd (L _ _ "IP_ADAPTER_INFO"))))))) _ -> return ()

CastExpr castTy (Fix (FunctionCall (Fix (VarExpr (L _ _ "malloc"))) [size])) -> do
checkType file "malloc" castTy
checkSize file "malloc" castTy size
checkType file "malloc" (removeOwner castTy)
checkSize file "malloc" (removeOwner castTy) size
CastExpr castTy (Fix (FunctionCall (Fix (VarExpr (L _ _ "mem_balloc"))) [_, size])) -> do
checkType file "mem_balloc" castTy
checkSize file "mem_balloc" castTy size
checkType file "mem_balloc" (removeOwner castTy)
checkSize file "mem_balloc" (removeOwner castTy) size

FunctionCall (Fix (VarExpr (L _ _ name))) _ | name `elem` mallocs ->
warn file node $ "the result of `" <> name <> "` must be cast; plain `void *` is not supported"
Expand Down
1 change: 1 addition & 0 deletions src/Tokstyle/Linter/TypeCheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ inferTypes = \case
TyBitwise ty -> return ty
TyForce ty -> return ty
TyConst ty -> return ty
TyOwner ty -> return ty
Ellipsis -> return T_Void

VLA ty (L _ _ name) size -> do
Expand Down
2 changes: 1 addition & 1 deletion src/Tokstyle/SemFmt/EnumFromInt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mkCase node = error $ show node

mkAssignOut :: Lexeme Text -> (LexemeClass, Text) -> Node (Lexeme Text)
mkAssignOut name (retCls, retStr) =
let outDeref = Fix (UnaryExpr UopDeref (Fix (VarExpr (mkLAt name IdVar "out")))) in
let outDeref = Fix (UnaryExpr UopDeref (Fix (VarExpr (mkLAt name IdVar "out_enum")))) in
Fix $ CompoundStmt
[ Fix (ExprStmt (Fix (AssignExpr outDeref AopEq (Fix (LiteralExpr ConstId name)))))
, Fix (Return (Just (Fix (LiteralExpr Bool (mkLAt name retCls retStr)))))
Expand Down
2 changes: 1 addition & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
packages: [.]
resolver: lts-21.9
extra-deps:
- cimple-0.0.20
- cimple-0.0.21
22 changes: 11 additions & 11 deletions test/Tokstyle/SemFmt/EnumFromIntSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ spec = do
, " FOO_ONE,"
, " FOO_TWO,"
, "} Foo;"
, "bool foo_from_int(int b, Foo *out) {"
, "bool foo_from_int(int b, Foo *out_enum) {"
, " switch (b) {"
, " case FOO_ONE: {"
, " *out = FOO_ONE;"
, " *out_enum = FOO_ONE;"
, " return true;"
, " }"
, " case FOO_TWO: {"
, " *out = FOO_ONE;" -- mistake here
, " *out_enum = FOO_ONE;" -- mistake here
, " return true;"
, " }"
, " default: {"
, " *out = FOO_ONE;"
, " *out_enum = FOO_ONE;"
, " return false;"
, " }"
, " }"
Expand All @@ -38,15 +38,15 @@ spec = do
[ "{"
, " switch (b) {"
, " case FOO_ONE: {"
, " *out = FOO_ONE;"
, " *out_enum = FOO_ONE;"
, " return true;"
, " }"
, " case FOO_TWO: {"
, " *out = FOO_TWO;"
, " *out_enum = FOO_TWO;"
, " return true;"
, " }"
, " default: {"
, " *out = FOO_ONE;"
, " *out_enum = FOO_ONE;"
, " return false;"
, " }"
, " }"
Expand All @@ -64,18 +64,18 @@ spec = do
, " FOO_ONE,"
, " FOO_TWO,"
, "} Foo;"
, "bool foo_from_int(int b, Foo *out) {"
, "bool foo_from_int(int b, Foo *out_enum) {"
, " switch (b) {"
, " case FOO_ONE: {"
, " *out = FOO_ONE;"
, " *out_enum = FOO_ONE;"
, " return true;"
, " }"
, " case FOO_TWO: {"
, " *out = FOO_TWO;"
, " *out_enum = FOO_TWO;"
, " return true;"
, " }"
, " default: {"
, " *out = FOO_ONE;"
, " *out_enum = FOO_ONE;"
, " return false;"
, " }"
, " }"
Expand Down
2 changes: 1 addition & 1 deletion tokstyle.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ library
, array <0.6
, base >=4 && <5
, bytestring <0.13
, cimple >=0.0.20
, cimple >=0.0.21
, casing <0.2
, containers <0.8
, data-fix <0.4
Expand Down
Loading