Working with the latest Alloy analyzer currently available at the website (4.2 Build date: 2012-09-25) I realized that, when I put code between two /**/ like this /**/ <some code> /**/, the code <some code> seems to be ignored by the analyzer (although the editor seems to parse the code correctly).
For example, in the following code snippet the declaration of the fact is ignored by the analyzer:
/**/
fact traces {
init [first]
all d: Dinner - last |
let d' = next [d] |
some p,p': Philosopher, f: Fork |
pickLeftFork [d,d',p,p',f]
}
/**/
As soon as I put a space between /**/, that is, /* */, the code behaves as expected.
Java-style doc comments have been recently added to Alloy, so in your example above, the /** token at the beginning starts a doc comment, and the **/ token at the very end closes is, so everything in between is parsed as a comment.
Related
Example:
ENSURE(strTitle.LoadString(AFX_IDS_APP_TITLE));
ENSURE(strMainInstruction.LoadString(IDS_STR_SUBMIT_STATS_MAIN_TEXT));
ENSURE(strContent.LoadString(IDS_STR_SUBMIT_STATS_CONTENT_TEXT));
ENSURE(strAdditional.LoadString(IDS_STR_SUBMIT_STATS_ADDITIONAL_TEXT));
ENSURE(strFooter.LoadString(IDS_STR_TASK_DIALOG_FOOTER));
ENSURE(strVerification.LoadString(IDS_STR_SUBMIT_STATS_VERIFICATION_TEXT));
ENSURE(strExpand.LoadString(IDS_STR_FIND_OUT_MORE));
ENSURE(strCollapse.LoadString(IDS_STR_COLLAPSE));
Definition:
#define ENSURE(cond) ENSURE_THROW(cond, ::AfxThrowInvalidArgException() )
It is a Microsoft macro, although I can't see it documented. I started using it when I noticed it being used in Microsoft SDK code. Annoyingly it triggers code analysis:
Warning C26496: The variable '__afx_condVal' does not change after construction, mark it as const (con.4).
I did raise it with Microsoft. The underlying macro ENSURE_THROW:
#define ENSURE_THROW(cond, exception) \
do { int __afx_condVal=!!(cond); ASSERT(__afx_condVal); if (!(__afx_condVal)){exception;} } __pragma(warning(suppress:4127)) while (false)
... it only needs the word const to resolve it.
Is there an alternative call I can make because I understand that ASSERT only works in DEBUG build.
You could redefine ENSURE_THROW and put this into your stdafx.h:
#undef ENSURE_THROW
#define ENSURE_THROW(cond, exception) \
do { const int __afx_condVal=!!(cond); ASSERT(__afx_condVal); \
if (!(__afx_condVal)){exception;} } while (false)
It's identical to the original MS definiton in afx.h, but with the const added.
This could break in future versions of MFC, although it is unlikely that MS will ever change this.
A cleaner way would be excluding certain header files from the analysis, but this depends on your tool.
Be aware that ENSURE and ASSERT are very different things:
ASSERT(x) : in debug builds it will stop the program execution with a message if x is false, in release builds it's a NOP, x won't even be evaluated
ENSURE(x) : x will be evaluated and if it's false an exception will be thrown. In debug builds a diagnostic dialog is shown before the exception is thrown.
I have a generated dart file in my project that has some unused methods -- these unused methods are causing the Dart Analysis server in Android Studio to give a warning about each unused method.
The warning looks like this:
info: The declaration '<method name>' isn't referenced. (unused_element at [<app name>] lib/Models/<file name>.g.dart:<line number of method>)
How does one suppress these warnings just for the generated file?
From Step 5 here (the whole article is well worth reading)
Warnings in generated files do not matter to you.
Generated files are out of your control. You shouldn’t edit them, and probably shouldn’t care about how the generated code looks like either.
As such, instead of polluting your IDE with tons of pointless warning, simply disabling the linter on generated files is enough. This can be done by adding some code to your analysis_options.yaml.
In our case, we will use both json_serializable and Freezed, so the code what we want to add is:
analyzer:
exclude:
# ignore warnings in files from json_serializable, built_value and most generators
- "**/*.g.dart"
# ignore warnings in files generated by Freezed specifically.
- "**/*.freezed.dart"
As a workaround it can be ignored with
// ignore: unused_element
_func_dart_complains_about() {}
or
// ignore_for_file: unused_element
...
_func_dart_complains_about() {}
This is a general issue with nested functions. If you're declaring a local function within a parent function, return the same function using a return statement for the parent function.
Like in the below example, if I wouldn't return max(); inside the parent void MainMax() I'd get the error The declaration '<method name>' isn't referenced. Do check and return proper nested functions.
void mainMax(int a, int b, int c) {
void max(){
if(a>b && a>c){
print (a);
} else if (b>a && b>c){
print (b);
} else{
print (c);
}
}
max();
}
I am testing the use of stop in a package using Rcpp and I see that when stop is used, the function exits into debug mode in R. For example, I am using the example function at the this link (see takeLog3 function, also pasted below)
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double takeLog3(double val) {
if (val <= 0.0) { // log() not defined here
stop("Inadmissible value");
}
return log(val);
}
The output of takeLog3(-10) is
> takeLog3(-10)
Error in takeLog3(-10) : Inadmissible value
Called from: takeLog3(-10)
Browse[1]>
and then I have to enter Q to exit out of debug mode. My question is, is that expected behavior? Secondly, how can I make stop statement to not go into debug mode, but quit the function completely after displaying the stop message?
Note that I see this behavior when takeLog3 is included in a package. I don't see this behavior when I have this function outside of a package in a simple .cpp file, where the function simply shows the stop message and control returns back to the console but does NOT go into debug mode.
Any help would be very much appreciated!
PS: I just found that I get the behavior I need by using Rf_error instead of stop. Can anyone provide an informed opinion, as to which one is preferred? Thank you.
Second question: stop() calls Rf_error() to implement the behaviour you see.
First question: You end up in the debugger because the IDE (which you didn't name or mention) wants to be helpful. Try it on the command-line in a standard R shell:
R> Rcpp::cppFunction("bool myStop(std::string reason) { Rcpp::stop(reason); }")
R> myStop("tired")
Error in myStop("tired") : tired
R>
Ubuntu 18.10, R 3.6.0.
I use Visual Studio Code and #types/node (7.0.8) but it seems that some functions etc. have a wrong formatted code comment and therefore Visual Studio Code and Visual Studio 2017 won't show any quickinfos in IntelliSense.
example
import * as fs from 'fs';
fs.unlink
When I type fs.unlink VS Code display the function signature but not the comment whitch is defined in
./node_modules/#types/node/index.d.ts
on line 2400
/*
* Asynchronous unlink - deletes the file specified in {path}
*
* #param path
* #param callback No arguments other than a possible exception are given to the completion callback.
*/
export function unlink(path: string | Buffer, callback?: (err?: NodeJS.ErrnoException) => void): void;
The culprit here is the first line which misses an asterisk.
The correct notation would be
/**
As soon as I change index.d.ts like that, I get working IntelliSense.
Some functions are commented corretly while others are not.
Am I doing something wrong here? (are those functions not meant to be used despite being exported)
Is it an error in #types/node and if so are there ways to teach VS Code to parse those comments?
Thanks
I work on TS and JS support for VSCode. The use of /* instead of /** looks like a bug in the node d.ts type definition. As far as I know, there is no way to configure TypeScript or VSCode to treat plain old /* comments as documentation comments.
I've submitted a PR with the fix: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/15285
For example, setting MYCONST = true would lead to the transformation of
if (MYCONST) {
console.log('MYCONST IS TRUE!'); // print important message
}
to
if (true) {
console.log('MYCONST IS TRUE!'); // print important message
}
This tool ideally has a fast node.js accessible API.
A better way to achieve what you want -
Settings.js
settings = {
MYCONST = true
};
MainCode.js
if (settings.MYCONST) {
console.log('MYCONST IS TRUE!'); // print important message
}
This way, you make a change to one single file.
google's closure compiler does, among other things, inlining of constants when annotated as such, leaving string content untouched but I am not sure if it's a viable option for you.
Patch a beautifier, for example
Get the JS Beautifier https://raw.github.com/einars/js-beautify/master/beautify.js written in JS.
Replace the last line of function print_token() by something like
output.push(token_text=="MYCONST"?"true":token_text);
Call js_beautify(your_code) from within nodejs.
The Apache Ant build system supports a replace task that could be used to achieve this.
Edit: Whoops. Gotta read title first. Ignore me.
Google Closure Compiler has such a feature:
You can combine the #define tag in your code with the --define parameter to change variables at "compile" time.
The closure compiler will also remove your if-statement, which is probably what you want.
Simply write your code like this:
/** #define {boolean} */
var MYCONST = false; // default value is necessary here
if (MYCONST) {
console.log('MYCONST IS TRUE!'); // print important message
}
And call the compiler with the parameter:
java -jar closure-compiler.jar --define=MYCONST=true --js pathto/file.js
Regarding you API request: Closure Compiler has a JSON API.