How to extract string constant in the source code using LLVM api - llvm

I want to extract string constants in the source code of a program.
For example, in the code snippet below:
char *s;
if(strcmp(s, "test")){
do something
}
"test" is the string I want to grab.
What's the easiest way to do that with LLVM?

Related

What is the easiest way to parse a json file using rapidjson?

So I am just getting familiar with rapidjson.h but I can't find this one basic piece of example code of parsing the *.json file.
I found the official [turorial][1].
But here however they parse the json stored in a C string. I know how this string is supposed to look like but I'm lazy to make a custom Parser just to convert my file to this string. I mean I was kinda hoping that rapidjson is supposed to do that for me. Please correct me if I'm wrong.
The closest thing I found to what I need is here How to read json file using rapidjson and output to std::string?
Therefore I was really surprised that I can't just do something like this (with *.json file being in the same folder as my program):
rapidjson::Document d;
d.Parse("myJson.json");
My 1. question is:
Do I have to use std::ifstream and rapidjson::IStreamWrapper to get my Document like in the example above and what other as simple as possible alternatives there are?
My 2. question is: (this one would be much easier to ask if i could comment the post above)
What does the R mean in std::ifstream ifs { R"(C:\Test\Test.json)" }; and how
do I change the C:\Test\Test.json string to const char* variable?
Because this isn't working.
const char* str = "C:\Test\Test.json";
std::ifstream ifs { R"(str)" }; //error
std::ifstream ifs { R(str) }; //error
std::ifstream ifs{ (str) }; //ok but I don't like it
[1]: https://rapidjson.org/md_doc_tutorial.html
You can use FileReadStream and ParseStream instead of the IStreamWrapper. According to the documentation, FileReadStream is much faster than IStreamWrapper.
The R means that it's a raw string literal. Without it, the backslashes are interpreted as the start of escape sequences and you would have to write it like this to make it correct:
"C:\\Test\\Test.json"
Or you could use forward slashes:
"C:/Test/Test.json"

List of all String Constants from GDB

I'm looking to analyze what information developers have put inside the source code as strings.
During the compilation process, is there a way to get GCC to output a list of all string constants used in a C++ programming. For example, if I write:
std::string result = enterLog("The secret word is: elephant");
result = enterLog("Checkpoint reached", "Now");
I would like to output
The secret word is: elephant
Checkpoint reached
Now
I would like to check to see if anyone has put the secret word in the code somewhere without having to check manually.

Roslyn - Parse string to MethodDeclarationSyntax

Is there any function in Roslyn, by which I can parse my string as a MethodDeclarationSyntax?
I have a file which contains the method declaration, so I am reading that file content to string and now I want to create a Method from that string. Any suggestions?
One option is to parse your string as "C# Script", an experimental version of C# that allows top-level method declarations. Doing:
var tree = SyntaxTree.ParseText("void Foo() {}", options: new ParseOptions(kind: SourceCodeKind.Script));
Gives you a valid syntax tree that contains a CompilationUnitSyntax of a single method declaration.

Get Non Indented string from Json Object

I have a json object in c++. I am using json_cpp library.
I want to get the string from the Json::Value object. I am using it like below.
Json::Value obj;
....
....
....
string str = obj.toStyledString();
This returns the string in the pretty print format. But I want the string without any indentation. How can I do that as there are no other functions provided in the class?
You could use Json::FastWriter it does not have any indentation and formatting since it outputs everything on a single line. it is normally not suitable for 'human' consumption.
std::string toUnStyledString(const Json::Value& value)
{
Json::FastWriter writer;
return writer.write( value );
}
The function toStyledString also simply uses a Json::StyledWriter if you look into the definition of Json::Value::toStyledString.
Well, if this library doesn't provide appropriate methods then you could write them yourself. The JSON format is rather simple, so I don't think that it will take a lot of work.
Here you can find a good graphical representation of JSON format:
http://json.org
P.S. I've never worked with this particular library, so I propose sort of a general solution.
UPDATE: another option is to get a string returned by toStyledString() and remove indentation. But it requires string processing and will probably be resource consuming. Note that you can't just remove tabs/spaces/new line symbols, because they can be a part of JSON object.
Why do you want unindented string again?

A macro to extract characters of its input text and generate a code using them

Is there any macro that can get a text as input like "abc" and by text I literally mean text like the one mentioned not an array or anything else, then extract characters of that text and generate a selective piece of code like ones below at compile time:
first example of a piece of code :
Func1(a);
Func2(b);
Func3(c);
second example of a piece of code:
{'a','b','c'}
You can't do it with a string "abc", but you can do it with (a,b,c). Look at boost preprocessor metaprogramming library: http://www.boost.org/doc/libs/1_44_0/libs/preprocessor/doc/index.html.