how to modify forStmt condtion by clang tools? - clang

I want to modify C source code like this:
I wany to modify loop condition in AST, how can I do this?
for example:
for(int i = 0; i < 10; i ++){...}
change to :
for (int i = 0; i < 1; i ++){...}
I get the forStmt from AST by ASTMatcher in clang tools and i also get the conditon:
auto cond = forstmt -> getCond();
if(cond){
if(auto bo = dyn_cast<BinaryOperator>(cond)){
auto borhs = bo -> getRHS();
if (auto condvar = dyn_cast<IntegerLiteral>(borhs)){
// how can i modify this condvar and dump ast to out.ast file?
string var_value = condvar -> getValue().toString(10, 1);
}
}
}

I got it from the Microsoft C++ Team Blog Exploring Clang Tooling Part 3: Rewriting Code with clang-tidy
Using clang-tidy and writing a clang tidy FixInt:CreatReplacement

Related

About extending a Look Up Table at compile time

I'd like to extend my instrumental Profiler in order to avoid it affect too much performances.
Im my current implementation, I'm using a ProfilerHelper taking one string, which is put whereever you want in the profiling f().
The ctor is starting the measurement and the dector is closing it, logging the Delta in an unordered_map entry, which is key is the string.
Now, I'd like to turn all of that into a faster stuff.
First of all, I'd like to create a string LUT (Look Up Table) contaning the f()s names at compile time, and turn the unordered_map to a plain vector which is paired by the string function LUT.
Now the question is: I've managed to create a LUT but std::string_view, but I cannot find a way to extend it at compile time.
A first rought trial sounds like this:
template<unsigned N>
constexpr auto LUT() {
std::array<std::string_view, N> Strs{};
for (unsigned n = 0; n < N; n++) {
Strs[n] = "";
}
return Strs;
};
constexpr std::array<std::string_view, 0> StringsLUT { LUT<0>() };
constexpr auto AddString(std::string_view const& Str)
{
constexpr auto Size = StringsLUT.size();
std::array<std::string_view, Size + 1> Copy{};
for (auto i = 0; i < Size; ++i)
Copy[i] = StringsLUT[i];
Copy[Size] = Str;
return Copy;
};
int main()
{
constexpr auto Strs = AddString(__builtin_FUNCTION());
//for (auto const Str : Strs)
std::cout << Strs[0] << std::endl;
}
So my idea should be to recall the AddString whenever needed in my f()s to be profiled, extending this list at compile time.
But of course I should take the returned Copy and replace the StringsLUT everytime, to land to a final StringsLUT with all the f() names inside it.
Is there a way to do that at compile time?
Sorry, but I'm just entering the magic "new" world of constexpr applied to LUT right in these days.
Tx for your support in advance.

How do I make a TImage move (like a DVD logo)

I am trying to make a TImage move like a DVD logo, but the TImage is not moving.
This is the code I used:
void __fastcall TForm1::DVDLogoTimer(TObject *Sender)
{
image->Left+=xPos; image->Top+=yPos;
if (image->Left <= invisibleHelperObject->Left) xPos=-xPos;
if (image->Top <= invisibleHelperObject->Top) yPos=-yPos;
if (image->Left+image->Width >= invisibleHelperObject->Width) xPos=-xPos;
if (image->Top+image->Height >= invisibleHelperObject->Height) yPos=-yPos;
Label1->Caption = IntToStr(xPos) + " | " + IntToStr(yPos);
}
(X and Y variables are not even changing (stays at 0))
In C++Builder 6 (and the "classic" Borland compiler in modern versions), you can't use compound operators like += with properties. Doing so will read the property value into a temporary and then modify the temporary, but will not assign the temporary back to the property. Using compound operators on properties requires a modern Clang-based compiler:
Differences Between Clang-enhanced C++ Compilers and Previous Generation C++ Compilers, __property: Compound and Chained Assignment
Clang-enhanced C++ compilers support compound assignment of __property, while BCC32 does not.
The objects of the keyword __property are not like fields or members. They should be used in simple assignments.
Although both BCC32 and the RAD Studio Clang-enhanced C++ compilers allow __property to be used in compound assignments such as:
Form1->Caption += DateToStr(Now());
BCC32 only invokes the getter, not the setter. Therefore we recommend that you avoid such constructs when targeting multiple platforms.
None of these compilers support the usage of __property in chained assignment, as in:
Button2->Caption = Button1->Caption = DateToStr(Now()); // Error
So, in your situation, when you invoke image->Left += xPos; for instance, it acts as-if you had written this instead:
//image->Left += xPos;
int temp = image->Left;
temp += xPos;
So, you need to use the + and = operators separately instead, eg:
void __fastcall TForm1::DVDLogoTimer(TObject *Sender)
{
image->Left = image->Left + xPos;
image->Top = image->Top + yPos;
...
}

How to ask Clang++ not to cache function result during -O3 optimization?

This is my code:
int foo(int x) {
return x + 1; // I have more complex code here
}
int main() {
int s = 0;
for (int i = 0; i < 1000000; ++i) {
s += foo(42);
}
}
Without -O3 this code works for a few minutes. With -O3 it returns the same result in no time. Clang++, I believe, caches the value of foo(42) (it's a pure function) and doesn't call it a million times. How can I instruct it NOT to apply this particular optimization for this particular function call?
Out of curiosity, can you share why you would want to disable that optimization?
Anyway, about your question:
In your example code, s is never read after the loop, so the compiler would throw the whole loop away. So let's assume that s is used after the loop.
I'm not aware of any pragmas or compiler options to disable a particular optimization in a particular section of code.
Is changing the code an option?
To prevent that optimization in a portable manner, you can look for a creative way to compute the function call argument in a way such that the compiler is no longer able to treat the argument as constant. Of course the challenge here is to actually use a trick that does not rely on undefined behavior and that cannot be "outsmarted" by a newer compiler version.
See the commented example below.
pro: you use a trick that uses only the language that you can apply selectively
con: you get an additional memory access in every loop iteration; however, the access will be satisfied by your CPU cache most of the time
I verified the generated assembly for your particular example with clang++ -O3 -S. The compiler now generates your loop and no longer caches the result. However, the function gets inlined. If you want to prevent that as well, you can declare foo with __attribute__((noinline)), for example.
int foo(int x) {
return x + 1; // I have more complex code here
}
volatile int dummy = 0; // initialized to 0 and never changed
int main() {
int s = 0;
for (int i = 0; i < 1000000; ++i) {
// Because of the volatile variable, the compiler is forced to assume
// that the function call argument is different for each loop
// iteration and it is no longer able to use a cached result.
s += foo(42 + dummy);
}
}

using a string in a math equation in Dart

I store various formulas in Postgres and I want to use those formulas in my code. It would look something like this:
var amount = 100;
var formula = '5/105'; // normally something I would fetch from Postgres
var total = amount * formula; // should return 4.76
Is there a way to evaluate the string in this manner?
As far as I'm aware, there isn't a formula solver package developed for Dart yet. (If one exists or gets created after this post, we can edit it into the answer.)
EDIT: Mattia in the comments points out the math_expressions package, which looks pretty robust and easy to use.
There is a way to execute arbitrary Dart code as a string, but it has several problems. A] It's very roundabout and convoluted; B] it becomes a massive security issue; and C] it only works if the Dart is compiled in JIT mode (so in Flutter this means it will only work in debug builds, not release builds).
So the answer is that unfortunately, you will have to implement it yourself. The good news is that, for simple 4-function arithmetic, this is pretty straight-forward, and you can follow a tutorial on writing a calculator app like this one to see how it's done.
Of course, if all your formulas only contain two terms with an operator between them like in your example snippet, it becomes even easier. You can do the whole thing in just a few lines of code:
void main() {
final amount = 100;
final formula = '5/105';
final pattern = RegExp(r'(\d+)([\/+*-])(\d+)');
final match = pattern.firstMatch(formula);
final value = process(num.parse(match[1]), match[2], num.parse(match[3]));
final total = amount * value;
print(total); // Prints: 4.761904761904762
}
num process(num a, String operator, num b) {
switch (operator) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
}
throw ArgumentError(operator);
}
There are a few packages that can be used to accomplish this:
pub.dev/packages/function_tree
pub.dev/packages/math_expressions
pub.dev/packages/expressions
I used function_tree as follows:
double amount = 100.55;
String formula = '5/105*.5'; // From Postgres
final tax = amount * formula.interpret();
I haven't tried it, but using math_expressions it should look like this:
double amount = 100.55;
String formula = '5/105*.5'; // From Postgres
Parser p = Parser();
// Context is used to evaluate variables, can be empty in this case.
ContextModel cm = ContextModel();
Expression exp = p.parse(formula) * p.parse(amount.toString());
// or..
//Expression exp = p.parse(formula) * Number(amount);
double result = exp.evaluate(EvaluationType.REAL, cm);
// Result: 2.394047619047619
print('Result: ${result}');
Thanks to fkleon for the math_expressions help.

Can clang static analysis be challenged with various flows

I wonder if currently there is support for clang static analysis with various flows checking. For example - there is a checker to check for zero division, but can clang find a flow which a divider can evaluate to zero ?
simple example: in the bellow example there is a flow (i=0) that b will evaluate to 0. do I get warning here ?
for(int i = 10; i>=0; i--){
int a = div(i);
...
}
int div(int b){
return 100000 / b;
}
if not, is there a plan to support this ?

Resources