-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add check that every declaration has a matching definition.
This avoids keeping function declarations around for which we deleted the implementation.
- Loading branch information
Showing
4 changed files
with
78 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE StrictData #-} | ||
module Tokstyle.Cimple.Analysis.DeclsHaveDefns (analyse) where | ||
|
||
import Control.Monad.State.Lazy (State) | ||
import qualified Control.Monad.State.Lazy as State | ||
import Data.Map (Map) | ||
import qualified Data.Map as Map | ||
import Data.Maybe (mapMaybe) | ||
import Data.Text (Text) | ||
import Language.Cimple (Lexeme (..), LexemeClass (..), | ||
Node (..)) | ||
import qualified Language.Cimple.Diagnostics as Diagnostics | ||
import Language.Cimple.TraverseAst (AstActions (..), defaultActions, | ||
traverseAst) | ||
import System.FilePath (takeFileName) | ||
|
||
|
||
data DeclDefn = DeclDefn | ||
{ decl :: Maybe (FilePath, Lexeme Text) | ||
, defn :: Maybe (FilePath, Lexeme Text) | ||
} | ||
|
||
|
||
collectPairs :: AstActions (State (Map Text DeclDefn)) Text | ||
collectPairs = defaultActions | ||
{ doNode = \file node act -> | ||
case node of | ||
FunctionDecl _ (FunctionPrototype _ fn@(L _ IdVar fname) _) _ -> do | ||
State.modify $ \pairs -> | ||
case Map.lookup fname pairs of | ||
Nothing -> Map.insert fname (DeclDefn{ decl = Just (file, fn), defn = Nothing }) pairs | ||
Just dd -> Map.insert fname (dd { decl = Just (file, fn) }) pairs | ||
act | ||
|
||
FunctionDefn _ (FunctionPrototype _ fn@(L _ IdVar fname) _) _ -> do | ||
State.modify $ \pairs -> | ||
case Map.lookup fname pairs of | ||
Nothing -> Map.insert fname (DeclDefn{ decl = Nothing, defn = Just (file, fn) }) pairs | ||
Just dd -> Map.insert fname (dd { defn = Just (file, fn) }) pairs | ||
act | ||
|
||
_ -> act | ||
} | ||
where | ||
|
||
analyse :: [(FilePath, [Node (Lexeme Text)])] -> [Text] | ||
analyse tus = | ||
map makeDiagnostic | ||
. mapMaybe lacksDefn | ||
. Map.elems | ||
. flip State.execState Map.empty | ||
. traverseAst collectPairs | ||
. filter (not . (`elem` ["ccompat.h", "tox.h"]) . takeFileName . fst) | ||
$ tus | ||
where | ||
lacksDefn DeclDefn{decl, defn = Nothing} = decl | ||
lacksDefn _ = Nothing | ||
|
||
makeDiagnostic (file, fn@(L _ _ fname)) = | ||
Diagnostics.sloc file fn <> ": missing definition for `" <> fname <> "'" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters