program that finds macros in source files

The program which should take file names from windows command line.
c:\mac_find main.cpp chars.h
the output of the program should print information like 1)total macros found 2)in which line the macros found and name of it.
example : if the file name is main.cpp, then the output should be
file:main.cpp
macros:3
macro at line:7 -> #define Test
macro at line:8 -> #define Play

If a file does not exist, then simply print an error message and continue searching for macros in other files.

Kindly help on writing the code.
Last edited on
Open file.
Read each line.
If line begins "#define", add to count of macros
When finished reading file, output the details of the recorded macros.

Start at the beginning. Write code to take the file name from the command line and open the file.
technically, I guess :)

not all macros are "macros".
#define unix
...
#ifdef unix
#include <unistd.h>
#endif

or
#ifndef dothisonce
#define dothisonce
code;
#endif


I would look for 3 words (2 spaces?) if you don't want every # define.
#define macro things
#define aconst 3.14

I think if the line has 3 or more words split by 2 or more spaces, its (probably) a real macro, if not, its part of a control statement. ??

just a thought. Even that may not be foolproof, but you can decide how much you want to get within what parameters.

Last edited on
In practice, I might just use grep, but if correctness is required it looks like one might be better off using the pre-processor directly.

Consider, e.g.:
1
2
3
// There is only one macro definition here
#define O(define) \
#define x  


Comments are stripped before the preprocessor runs, which means that you must account for commented-out macros, too. This requires accounting for all the little pathologies of C++'s comments. And string literals.

GNU-compatible preprocessors support the -d* family of flags, which may be useful:
https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html#dletters

Consider reading your preprocessor's manual to see if there's enough functionality to do this built-in.
Last edited on
As mbozzi said, there is already a program, the preprocessor, that "understands" macros. For example:
https://stackoverflow.com/questions/2224334/gcc-dump-preprocessor-defines
https://social.msdn.microsoft.com/Forums/vstudio/en-US/162e8850-f442-4283-a419-6c328684388e/showing-how-a-macro-is-expanded?forum=vclanguage

I need to write The program

Does that mean that you don't actually care about the macros, but rather have a "text parser" homework?
Hmm. I have no idea how to run just the preprocessor! I know I could lift it out of a compiler's source code, but not sure where it lives in my tools. A great solution, though. I approve of less work!
There's a flag for it (-E, I think).
Thank you for the suggestions. I'm to new to this command line input in C++. kindly help !!
Here is a skeleton to get you started.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

struct MacroInfo
{
  int line;
  string name;
};

vector<MacroInfo> ProcessFile(const char *filename);
void PrintMacroInfos(const char *filename, vector<MacroInfo> macros);

int main(int argc, char *argv[])
{
  for (int i = 1; i < argc; i++)
  {
    vector<MacroInfo> macros = ProcessFile(argv[i]);
    PrintMacroInfos(argv[i], macros);
  }
}

vector<MacroInfo> ProcessFile(const char *filename)
{
  vector<MacroInfo> macros;

  // TODO read the file line by line and if it contains a macro
  // create a MacroInfo, fill it with the line and macro name
  // add it to the vector macros

  return macros;
}

void PrintMacroInfos(const char *filename, vector<MacroInfo> macros)
{
  // TODO print the macros to the console
}

Now it's up to you to do some work.
Topic archived. No new replies allowed.