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

IWYU support (part 3): Improve Visual Studio compatibility #27785

Merged
merged 4 commits into from
Jan 22, 2019
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
4 changes: 1 addition & 3 deletions src/explosion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "game.h"
#include "item_factory.h"
#include "json.h"
#include "math_defines.h"
#include "map.h"
#include "messages.h"
#include "output.h"
Expand All @@ -23,9 +24,6 @@
#include "vehicle.h"
#include "vpart_position.h"

// For M_PI
#define _USE_MATH_DEFINES
#include <cmath>
#include <queue>
#include <random>

Expand Down
17 changes: 17 additions & 0 deletions src/math_defines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef CATA_MATH_DEFINES_H
#define CATA_MATH_DEFINES_H

// On Visual Studio math.h only provides the defines (M_PI, etc.) if
// _USE_MATH_DEFINES is defined before including it.
// We centralize that requirement in this header so that IWYU can force this
// header to be included when such defines are used, via mappings.
// At time of writing only M_PI has a mapping, which is the most commonly used
// one. If more are needed, add them to tools/iwyu/vs.stdlib.imp.

// Note that it's important to use math.h here, not cmath. See
// https://stackoverflow.com/questions/6563810/m-pi-works-with-math-h-but-not-with-cmath-in-visual-studio/6563891

#define _USE_MATH_DEFINES
#include <math.h>

#endif // CATA_MATH_DEFINES_H
8 changes: 5 additions & 3 deletions src/posix_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
#ifndef TIME_SPEC_H
#define TIME_SPEC_H

// Compatibility header. On POSIX, just include <ctime>. On Windows, provide
// our own nanosleep implementation.

#include <ctime> // IWYU pragma: keep

#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
/* Windows platforms. */

/* Windows lacks the nanosleep() function. The following code was stuffed
together from GNUlib (http://www.gnu.org/software/gnulib/), which is
licensed under the GPLv3. */

#include <cerrno>
#include <ctime>

enum { BILLION = 1000 * 1000 * 1000 };

# ifdef __cplusplus
Expand Down
6 changes: 2 additions & 4 deletions src/rng.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#include "rng.h"

#include <chrono>

#include "output.h"

#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdlib>
#include <random>

#include "output.h"

long rng( long val1, long val2 )
{
long minVal = ( val1 < val2 ) ? val1 : val2;
Expand Down
2 changes: 2 additions & 0 deletions src/sdltiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
# ifndef strcasecmp
# define strcasecmp StrCmpI
# endif
#else
# include <strings.h> // for strcasecmp
#endif

#ifdef __ANDROID__
Expand Down
1 change: 1 addition & 0 deletions tools/iwyu/cata.imp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[
{ ref: "gcc.stdlib.imp" },
{ ref: "vs.stdlib.imp" },
{ ref: "sdl.imp" },
]
1 change: 1 addition & 0 deletions tools/iwyu/gcc.stdlib.imp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
{ include: ["<ext/new_allocator.h>", "private", "<unordered_set>", "public"] },
{ include: ["<debug/array>", "private", "<array>", "public"] },
{ include: ["<debug/bitset>", "private", "<bitset>", "public"] },
{ include: ["<debug/deque>", "private", "<deque>", "public"] },
{ include: ["<debug/vector>", "private", "<vector>", "public"] },
{ include: ["<debug/list>", "private", "<list>", "public"] },
{ include: ["<debug/map.h>", "private", "<map>", "public"] },
Expand Down
5 changes: 5 additions & 0 deletions tools/iwyu/vs.stdlib.imp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{ symbol: ["M_PI", "private", "\"math_defines.h\"", "public"] },
{ symbol: ["nanosleep", "private", "\"posix_time.h\"", "public"] },
{ symbol: ["timespec", "private", "\"posix_time.h\"", "public"] },
]