Is there a LLVM Matcher for any conditional? - clang-ast-matchers

Is there a LLVM AST matcher for the use of a C conditional? I know there is the hasCondition() option for ifStmt, but that is only good for an if statement. In particular, I'm looking to match for a boolean condition that has no operator (e.g. if (flag), while(flag), or (flag ? x : y)). But I'd also be interested in the more generic case of any conditional.

The closest I could find was for ifStmt, whileStmt or doStmt:
xxxStmt(unless(hasCondition(binaryOperator(isComparisonOperator()))))
which allows me to also check for things like if (!flag)
For the case flag ? x : y, one can match conditionalOperator() then determine if the expression has a comparison operator.

Related

Difference between sequential and combined predicates

In Selenium I have written a xpath and both of them retrieves the same result.
//a[#role='tab'][text()=' Assets']
//a[#role='tab' and text()=' Assets']
Does both of them have the same meaning?
In most cases a[b][c] has exactly the same effect as a[b and c]. There are two exceptions to be aware of:
They are not equivalent if either predicate is numeric, or has a dependency on position() or last() (I call these positional predicates). For example a[#x][1] selects the first a element that has an #x attribute, while a[1][#x] selects the first a element provided it has an #x attribute (and selects nothing otherwise). By contrast a[1 and #x] converts the integer 1 to the boolean true(), so it just means a[#x].
There may be differences in behaviour if evaluation of b or c fails with a dynamic error. The precise rules here depend on which version of XPath you are using, and to be honest the rules leave implementations some leeway, but you need to exercise care if you want to be sure that in the event of b being false, c is not evaluated. (This hardly matters in XPath 1.0 because very few expressions throw dynamic errors.)
When you add Square Brackets ([]) to XPath you are adding a condition, so
first row adding 2 conditions
Which produce similar results as adding condition with and
Normally you don't use first row, because it less readable,
Mainly because this syntax represent in other languages a Matrix
// return a random m-by-n matrix with values between 0 and 1
public static double[][] random(int m, int n) {
See tutorial:
5 XPaths with predicates
A predicate is an expression that can be true or false
It is appended within [...] to a given location path and will refine results
More than one predicate can be appended to and within (!) a location path
The first one is a predicate, which means it checks if a[#role='tab'] is true then it proceeds to [text()=' Assets']
The second one is a just using an and operator so it validates both are true.

Limit operator (if variable>value then variable=value)

Is there such a thing as a Limit operator that lets you control the maximum or minium value of a variable.
if variable > value then variable = value
My question is not language specific, but answers in different languages are appreciated (esp. Delhpi).
I know operators differ from language to language, but mostly in syntax.
Would an operator like this be usefull enough?
Some languages have "min" operator that can be used for this: variable = min(variable, limit)
Basically, an operator is nothing else than a function.
Unary operators like ! (not) can be mapped with the function
Boolean not(Boolean)
Binary operators like + (plus) can be mapped with the function
Integer plus(Integer, Integer)
...
So, you can define any missing "operator" on your own as a function. Many languages don't allow to define operators on your own. In Groovy you can overload existing operators:
http://groovy.codehaus.org/Operator+Overloading

Do shortcut operators work in objective C?

In MATLAB, if you type if (a)&&(b), then b isn't tested if a is false. This is really handy for cases where the second condition would throw an error if the first condition isn't true.
In Objective C, this doesn't appear to be the case. So, my question is: are there any similar ways to have ordered conditions in an if statement? Or must the two statements simple be nested? (E.g., if (a){ if(b) {.)
Thanks for reading.
In Objective C, this doesn't appear to be the case.
This is incorrect. C and its derivatives, including C++ and Objective-C, follow the same short-circuiting rules that you're familiar with from Matlab. In an expression of the form:
x && y
y will not be evaluated if x is false.

inactive ? #"inactive": #"active" syntax?

Can someone explain the inactive ? #"inactive": #"active"?
The ? : is a boolean conditional structure (wrong term) it seems but I'm not quite getting it. Don't know what it is called so can't look it up.
Seems something like:
someBooleanValue ? if it is false use what is before colon : else use
what is after
I get that it is being used to determine which string to use as the format token (in the code below). I just don't know what this ? : bit is called and what limitations/cautions/abuses there may be with it.
(and isn't ObjC like rilly hard to format in a civilized way)
UIAlertView* av = [[UIAlertView alloc] initWithTitle:#"Hey"
message:[NSString
stringWithFormat:#"While %#, I received a local notification: %#",
inactive ? #"inactive": #"active", n.alertBody]
delegate:nil
cancelButtonTitle:#"OK" otherButtonTitles:nil];
[av show];
This ?: thing is called a conditional operator or a ternary operator.
It's represents a simple condition
if ( CONDITION )
x = a;
else
x = b;
that can be translated to
x = CONDITION ? a : b
From that you can probably derive the functionality you're trying to accomplish/understand. Keep in mind that, although you could probably use it as a substitute to the normal if/else-if/else structure, it is considered bad programming the usage of the ternary operator out of any "assignment related action".
In the wikipedia page for it you can find a great variety of examples of the conditional operators used in different programming languages. Check this one too, the ternary operator page.
Obs: turns out that a ternary operator is not necessarily a conditional expression, but rather any operator that takes three arguments. Since for most of the programming languages the only ternary operator is the inline-if... well, that's what it's usually called.
It is called conditional operator, a kind of ternary operator (as opposed to more familiar binary a+b or unary !flag operators).
Conditional operator takes a condition, evaluates it, and returns its second or third operand depending on that result.
You can read more information here.
The use of this operator can greatly reduce code length when a lot of simple ifs are involved.
It's a ternary operator, but you have it backwards -- if the boolean is true, then do the thing before the colon, otherwise, the one after.
This is called the ternary operator and it works exactly the way you described it:
expression ? value if true : value if false;
For instance, you could use it for something like this to avoid a if - else:
int maxValue = a > b ? a : b;
Edit: #dasblinkenlight is correct, the operator you're talking about is actually called a conditional operator, which is a kind of ternary operator.

What advantages are there to using either AND or &&?

Currently, I'm using && and || instead of AND and OR because that's how I was taught. In most languages, however, both are valid syntax. Are there any advantages to one or the other in any language?
I did try to search for this question, but it's a bit hard. It doesn't interpret my input correctly.
You ask “Are there any advantages to one or the other in any language?” This is, of course, dependent on the programming language.
Languages that implement both and and && (and correspondingly or and ||) will do it one of two ways:
Both behave exactly the same way. In which case, there is no advantage provided by the language in using one over the other.
Each behaves differently. In which case, the advantage is that you can get different behaviour by using one or the other.
That all sounds a bit facetious, but it's really as specific as one can get without talking about a specific language. Your question explicitly wants to know about all languages, but it's a question that needs to be answered per language.
Perl has all four of {&& || and or} but they differ in their precedence. "and" and "or" have really low precedence so you can do things like "complex-function-call-here or die $!" and you won't accidentally have "or" slurp up something on its left side that you didn't want it to.
it depends on the language, but on PHP, I'd be careful about using && versus "and". The ones i often use are "&&" and "||"
http://us3.php.net/manual/en/language.operators.logical.php
$g = true && false; // $g will be assigned to (true && false) which is false
$h = true and false; // $h will be assigned to true
In some languages && will have a higher operator precedence than AND.
If both works fine, then I would say it's really personal preference, in most cases, they are compiled into same binary code like this : 11100010001000101001001010 [not real code, just an example].
&& = two keystrokes of the same key.
AND = three keystrokes of different keys.
I'm not sure what language you are using, but some languages differentiate between a normal boolean operator and a short-circuit operator. For example, the following are normal boolean operators in MATLAB:
C = or(A,B);
C = A | B; % Exactly the same as above
However, this is a short-circuit operator:
C = A || B;
The short-circuit syntax will evaluate the first argument and then, depending on the value, will potentially skip over evaluating the second argument. For example, if A is already true, B doesn't have to be evaluated for an OR operation, since the result is guaranteed to be true. This is helpful when B is replaced with a logical operation that involves some kind of expensive computation.
Here's a wikipedia link discussing short-circuit operators and their syntax for a few languages.
Unless there aren't any precedence issues, I'd say there are differences in readability. Consider the following:
if (&x == &y && &y == &z) {
// ..
}
#define AND &&
if (&x == &y AND &y == &z) {
// ..
}