Skip to content

Commit ae5c6d3

Browse files
ndmitchellcocreature
authored andcommitted
Move the definition of __GHCIDE__ (#377)
* Move the definition of __GHCIDE__ so we don't modify upstream copied code * Add a test that __GHCIDE__ works
1 parent c660830 commit ae5c6d3

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

Diff for: src/Development/IDE/Core/Preprocessor.hs

+8
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,18 @@ runLhs dflags filename contents = withTempDir $ \dir -> do
167167
escape [] = []
168168

169169

170+
modifyOptP :: ([String] -> [String]) -> DynFlags -> DynFlags
171+
modifyOptP op = onSettings (onOptP op)
172+
where
173+
onSettings f x = x{settings = f $ settings x}
174+
onOptP f x = x{sOpt_P = f $ sOpt_P x}
175+
170176
-- | Run CPP on a file
171177
runCpp :: DynFlags -> FilePath -> Maybe SB.StringBuffer -> IO SB.StringBuffer
172178
runCpp dflags filename contents = withTempDir $ \dir -> do
173179
let out = dir </> takeFileName filename <.> "out"
180+
dflags <- pure $ modifyOptP ("-D__GHCIDE__":) dflags
181+
174182
case contents of
175183
Nothing -> do
176184
-- Happy case, file is not modified, so run CPP on it in-place

Diff for: src/Development/IDE/GHC/CPP.hs

-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ doCpp dflags raw input_fn output_fn = do
116116
++ map SysTools.Option sse_defs
117117
++ map SysTools.Option avx_defs
118118
++ mb_macro_include
119-
-- Define a special macro "__GHCIDE__"
120-
++ [ SysTools.Option "-D__GHCIDE__"]
121119
-- Set the language mode to assembler-with-cpp when preprocessing. This
122120
-- alleviates some of the C99 macro rules relating to whitespace and the hash
123121
-- operator, which we tend to abuse. Clang in particular is not very happy

Diff for: test/exe/Main.hs

+29-16
Original file line numberDiff line numberDiff line change
@@ -1281,22 +1281,35 @@ pluginTests = testSessionWait "plugins" $ do
12811281

12821282
cppTests :: TestTree
12831283
cppTests =
1284-
testCase "cpp" $ do
1285-
let content =
1286-
T.unlines
1287-
[ "{-# LANGUAGE CPP #-}",
1288-
"module Testing where",
1289-
"#ifdef FOO",
1290-
"foo = 42"
1291-
]
1292-
-- The error locations differ depending on which C-preprocessor is used.
1293-
-- Some give the column number and others don't (hence -1). Assert either
1294-
-- of them.
1295-
(run $ expectError content (2, -1))
1296-
`catch` ( \e -> do
1297-
let _ = e :: HUnitFailure
1298-
run $ expectError content (2, 1)
1299-
)
1284+
testGroup "cpp"
1285+
[ testCase "cpp-error" $ do
1286+
let content =
1287+
T.unlines
1288+
[ "{-# LANGUAGE CPP #-}",
1289+
"module Testing where",
1290+
"#ifdef FOO",
1291+
"foo = 42"
1292+
]
1293+
-- The error locations differ depending on which C-preprocessor is used.
1294+
-- Some give the column number and others don't (hence -1). Assert either
1295+
-- of them.
1296+
(run $ expectError content (2, -1))
1297+
`catch` ( \e -> do
1298+
let _ = e :: HUnitFailure
1299+
run $ expectError content (2, 1)
1300+
)
1301+
, testSessionWait "cpp-ghcide" $ do
1302+
_ <- openDoc' "A.hs" "haskell" $ T.unlines
1303+
["{-# LANGUAGE CPP #-}"
1304+
,"main ="
1305+
,"#ifdef __GHCIDE__"
1306+
," worked"
1307+
,"#else"
1308+
," failed"
1309+
,"#endif"
1310+
]
1311+
expectDiagnostics [("A.hs", [(DsError, (3, 2), "Variable not in scope: worked")])]
1312+
]
13001313
where
13011314
expectError :: T.Text -> Cursor -> Session ()
13021315
expectError content cursor = do

0 commit comments

Comments
 (0)