on master help sign in
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

commitae5c6d34d470e2fd47a21875b2244c70479fd629 authorNeil Mitchell <ndmitchell@gmail.com> dateMon 27 Jan 2020, 11:57 (7 years ago) parentc660830
3 files changed+37−18
src/Development/IDE/Core/Preprocessor.hs+8 src/Development/IDE/GHC/CPP.hs−2 test/exe/Main.hs+29−16
src/Development/IDE/Core/Preprocessor.hs+8
@@ -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
171177runCpp :: DynFlags -> FilePath -> Maybe SB.StringBuffer -> IO SB.StringBuffer
172178runCpp 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
src/Development/IDE/GHC/CPP.hs−2
@@ -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
test/exe/Main.hs+29−16
@@ -1281,22 +1281,35 @@ pluginTests = testSessionWait "plugins" $ do
12811281
12821282cppTests :: TestTree
12831283cppTests =
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