0

Say I have the following code which contains a string

const char* fmt = L"%s";

int main()
{
    printf(fmt, L"hello");
}

Before I compile, I'd like an event to occur which transforms my source code, such that all strings are wrapped by a function. The final code should be like

const char* fmt = w2mb(L"%s");

int main()
{
    printf(fmt, w2mb(L"hello"));
}

Intelisense can identify strings so I know it is possible. How can I go about doing this? I figure it relates to pre-build events.

jan.sende
  • 750
  • 6
  • 23
man of asdf
  • 111
  • 7
  • I cannot really understand what you are asking. Please rephrase your question! That being said, it seems like you are searching for something like https://github.com/fmtlib/fmt or https://abseil.io/docs/cpp/guides/format Plus, you maybe want logging sooner or later, thus I recommend having a look at https://github.com/gabime/spdlog (which uses fmt). – jan.sende Aug 06 '19 at 01:08
  • 1
    Why don't you use `wprintf`, or drop `L` prefixes? I'm not sure how you are even going to implement `w2mb` in a way that doesn't leak memory. This sounds line an [XY problem](http://xyproblem.info/) – Igor Tandetnik Aug 06 '19 at 01:13
  • By the way, this looks more like C, not C++. –  Aug 06 '19 at 02:54
  • `This will obviously not compile.` What compile errors are you getting? Are you asking solely about the compile errors? –  Aug 06 '19 at 03:17
  • @jan.sende It has nothing to do with the strings themselves, but an operation done to my source code before compilation – man of asdf Aug 07 '19 at 21:52
  • @IgorTandetnik this is an example, please focus on the question – man of asdf Aug 07 '19 at 21:52
  • @Chipster I am working in C++ so I specified that. The fact that the code works in C is just coincidence – man of asdf Aug 07 '19 at 21:52
  • @Chipster Type errors. a `char*` cannot be cast as a `wchar_t*` implicitly – man of asdf Aug 07 '19 at 21:53
  • @manofasdf Could you clarify what you mean by prebuild events? –  Aug 07 '19 at 21:58
  • @Chipster That's literally the question. Visual Studio has a project property that is "prebuild event" and I'd like to know how it exactly works and if it's usable in my situation. – man of asdf Aug 07 '19 at 21:59
  • *"please focus on the question"* Well, when one is faced with an [XY problem](http://xyproblem.info/), the wise course of action is in fact to not focus on the immediate question (the Y), but to try and figure out what the questioner really wants to achieve (the X). What you are asking for is a really weird thing to want to do; there's likely a better, easier way to solve your real problem, if only you would explain what it is. – Igor Tandetnik Aug 08 '19 at 01:30
  • I made an edit. What you want is a pre-build transformation. (A transformation before the build.) Not a prebuild event. (An already built event.) My question is... Why? What do you want achieve with this? Why not just start with the already transformed code? – jan.sende Aug 08 '19 at 05:54

1 Answers1

1

Pre-build events are just command-line commands that are run prior to running the compiler. If you want to wrap all the string constants in your program with a function you're going to need to write a preprocessor that carries this out. That, in turn, is going to require hooking into a C++ compiler frontend. I know that Roslyn makes this sort of thing fairly nice for C# but I don't know what's available for C++. You might look into clang, which I think is what Visual Studio Code uses for its plugins; as far as I know, MSVC isn't an open-source compiler.

Of course if you're working with a limited selection of files formatted in particularly nice ways you might not need the full power of a C++ parser; you might even be able to get away with a regex or something.

That said this seems like a pretty weird thing to do, and you might want to consider whether you can rearrange things to avoid having to do this.

Daniel McLaury
  • 4,047
  • 1
  • 15
  • 37
  • "If you want to wrap all the string constants in your program with a function you're going to need to write a preprocessor that carries this out" Could you elaborate on this? – man of asdf Aug 07 '19 at 21:51
  • You'll have to write your own program, likely from scratch, that carries out that transformation on your source files. – Daniel McLaury Aug 07 '19 at 21:52
  • You could do that, but I'd recommend having it produce temporary files with a different extension or something instead. (That said, what I'd *actually* recommend is not doing this at all -- why do you want a bunch of .cpp files that won't compile lying around?) – Daniel McLaury Aug 07 '19 at 21:59
  • They'll compile. Prebuild duplicates `Source.cpp` into `Source.cpp.bak` and then the original `Source.cpp` is modified like is desired in the question. Compilation ensues. Postbuild deletes `Source.cpp` and renames `Source.cpp.bak` back into `Source.cpp`. Now to somehow make a way to identify strings from a file... :P – man of asdf Aug 07 '19 at 22:03
  • I agree that that is a viable way to do what you're asking to do, although from a software engineering perspective it's not a course of action I recommend. – Daniel McLaury Aug 07 '19 at 22:07