3

I have a large body of C code (dozens of files that are several thousand lines each) that I'm trying to port from another platform to MSVC++. There are many redundant types in a file because of sloppiness of previous programmers.

Obviously I can eliminate the types one by one and see if they build. Is there a quick way in Visual Studio that I can identify or refactor out unused types?

typedef struct {
    int Field1;
    int Field2;
} notused1;

struct notused2 {
    int Field1;
    int Field2;
};

int ActualWork() {
    // Doesn't use either struct
}

I don't believe this question is a duplicate of 2380153 because that question was asked in 2010.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
John Mott
  • 119
  • 1
  • 12
  • Possible duplicate of [Finding "dead code" in a large C++ legacy application](https://stackoverflow.com/questions/2380153/finding-dead-code-in-a-large-c-legacy-application) – Acorn Feb 03 '19 at 15:50
  • Despite your protestations that a question asked in 2010 is not relevant to you, the situation has not changed significantly in the last 9 years or so, or for the 20-odd years before that. There isn't a simple automatic way to do it. There are static analyzers of greater or lesser excellence that may or may not be useful, but the process is still exasperating and not far removed from 'hit or miss'. – Jonathan Leffler Feb 03 '19 at 17:03
  • https://stackoverflow.com/questions/38822663/only-keep-used-types-and-remove-unused-types - this may be closer to your case, please share your feedback if you decide to use code-cleaning tools with Visual Studio – Baj Mile Feb 03 '19 at 23:51
  • Does this work for C? This looks like managed code. – John Mott Feb 04 '19 at 03:57
  • This looks promising: https://www.jetbrains.com/resharper-cpp/?fromMenu – John Mott Feb 04 '19 at 04:01

1 Answers1

4

In Visual Studio, you can deprecate all the symbols and get warnings for each used symbol. Then remove all deprecation off from used symbols, and the rest are not used.

like this:

#pragma deprecated(X)
struct X {  // will result C4995 warning in visual studio when using this struct
};

You can also use __declspec(deprecated) like this:

struct __declspec(deprecated) X {

};

look here and here

SHR
  • 7,940
  • 9
  • 38
  • 57
  • 1
    This is a pretty creative solution in my opinion :) – Morten Jensen Feb 04 '19 at 09:08
  • 1
    I've accepted this as the answer, because it is a visual studio solution, and while its manual its very light touch. Thank you! – John Mott Feb 04 '19 at 14:05
  • For anyone in the future I've also found the clang program can generate an AST tree of a C program with -ast-dump. This output can then be parsed to build a representation of the program, which can be used for static analysis. Many more steps but something that can go into the long term toolkit – John Mott Feb 08 '19 at 15:32