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

Add line marker after macro expansion #27

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion cpphs-1.20.9/Language/Preprocessor/Cpphs/MacroPass.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Language.Preprocessor.Cpphs.Tokenise (tokenise, WordStyle(..)
, parseMacroCall)
import Language.Preprocessor.Cpphs.SymTab (SymTab, lookupST, insertST
, emptyST, flattenST)
import Language.Preprocessor.Cpphs.Position (Posn, newfile, filename, lineno)
import Language.Preprocessor.Cpphs.Position (Posn, newfile, filename, lineno, cppline, newline)
import Language.Preprocessor.Cpphs.Options (BoolOptions(..))
import System.IO.Unsafe (unsafeInterleaveIO)
import Control.Monad ((=<<))
Expand Down
44 changes: 30 additions & 14 deletions cpphs-1.20.9/Language/Preprocessor/Cpphs/Tokenise.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ data SubMode = Any | Pred (Char->Bool) (Posn->String->WordStyle)
-- * Other is anything else.
data WordStyle = Ident Posn String | Other String | Cmd (Maybe HashDefine)
deriving (Eq,Show)

other :: Posn -> String -> WordStyle
other _ s = Other s
other _ = Other

lineMarker :: Posn -> WordStyle
lineMarker p = Cmd (Just (LineDrop (cppline p)))

deWordStyle :: WordStyle -> String
deWordStyle (Ident _ i) = i
Expand Down Expand Up @@ -265,17 +269,29 @@ tokenise stripEol stripComments ansi lang ((pos,str):pos_strs) =

-- | Parse a possible macro call, returning argument list and remaining input
parseMacroCall :: Posn -> [WordStyle] -> Maybe ([[WordStyle]],[WordStyle])
parseMacroCall p = call . skip
parseMacroCall p = uncurry call . skipSpaces 0
where
skip (Other x:xs) | all isSpace x = skip xs
skip xss = xss
call (Other "(":xs) = (args (0::Int) [] [] . skip) xs
call _ = Nothing
args 0 w acc ( Other ")" :xs) = Just (reverse (addone w acc), xs)
args 0 w acc ( Other "," :xs) = args 0 [] (addone w acc) (skip xs)
args n w acc (x@(Other "("):xs) = args (n+1) (x:w) acc xs
args n w acc (x@(Other ")"):xs) = args (n-1) (x:w) acc xs
args n w acc ( Ident _ v :xs) = args n (Ident p v:w) acc xs
args n w acc (x@(Other _) :xs) = args n (x:w) acc xs
args _ _ _ _ = Nothing
addone w acc = reverse (skip w): acc
skipSpaces newlineCount (Other x:xs)
| all isSpace x = skipSpaces (newlineCount + countNewlines x) xs
skipSpaces newlineCount xss = (newlineCount,xss)
call newlineCount (Other "(":xs) = args (0::Int) newlineCount [] [] xs
call _ _ = Nothing
args 0 newlineCount w acc ( Other ")" :xs) =
let (newlineCount',w') = skipSpaces newlineCount w
in
-- we add a line marker at the end of the macro call, that positions the
-- the following code after all newlines in the macro call
Just (reverse (addone w' acc), lineMarker (newlines (newlineCount'+1) p):xs)
-- we have to add an additional `1` because the line marker has to end with a newline
args 0 newlineCount w acc ( Other "," :xs) =
let (newlineCount1,w') = skipSpaces newlineCount w
(newlineCount2,xs') = skipSpaces newlineCount1 xs
in
args 0 newlineCount2 [] (addone w' acc) xs'
args n newlineCount w acc (x@(Other "("):xs) = args (n+1) newlineCount (x:w) acc xs
args n newlineCount w acc (x@(Other ")"):xs) = args (n-1) newlineCount (x:w) acc xs
args n newlineCount w acc (x@(Other y ):xs) = args n (newlineCount+countNewlines y) (x:w) acc xs
args n newlineCount w acc ( Ident _ v :xs) = args n newlineCount (Ident p v:w) acc xs
args _ newlineCount _ _ _ = Nothing
addone w acc = reverse w:acc
countNewlines = length . filter (=='\n')