Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 343274 - [checker]: Checker on the ability of class methods to be constant or static
Summary: [checker]: Checker on the ability of class methods to be constant or static
Status: NEW
Alias: None
Product: CDT
Classification: Tools
Component: cdt-codan (show other bugs)
Version: 8.0   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: CDT Codan Inbox CLA
QA Contact: Elena Laskavaia CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-04-19 10:33 EDT by Anton G. CLA
Modified: 2019-10-07 14:37 EDT (History)
3 users (show)

See Also:


Attachments
Basic checker implementation (26.32 KB, patch)
2011-04-19 10:35 EDT, Anton G. CLA
no flags Details | Diff
Fixed checker implementation (see comment for details) (28.39 KB, patch)
2011-05-02 04:46 EDT, Anton G. CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Anton G. CLA 2011-04-19 10:33:06 EDT
Consider a few examples first:

struct My
{
    int value;
    // Case 1: getValue() should be constant cause it does not write class members
    void getValue() { return value };

    // Case 2: increment() should be static cause it does not use class members.
    int increment(int _value) { return _value+1; }

    // Case 3: Class member 'value' cannot be changed in constant method getNewValue().
    int getNewValue() const { return ++value; }

    // Case 4: Static method decrement() cannot access class member 'value'.
    static void decrement() { return --value; }
};


So checker reports a problem on the next cases:
  1. class method should be constant (there is no writing to the class members
  and calls of another non-constant methods);
  2. class method should be static (there is no access to the class members);
  3. class member is written in a constant method;
  4. class member is accessed in a static method;

NOTES:
  - There is no warning for virtual methods in cases 1 & 2 (cause their signature is derived from base classes and probably cannot be changed).
  - There is no check if non-constant method is called from constant one cause CDT Indexer does not resolve it correctly (and problem binding checker reports an error)
Comment 1 Anton G. CLA 2011-04-19 10:35:51 EDT
Created attachment 193586 [details]
Basic checker implementation

Can somebody review and commit or comment my patch?
Comment 2 Sergey Prigogin CLA 2011-04-25 23:46:11 EDT
(In reply to comment #1)

It would be nice if the logic implemented in this checker was also applied to content assist. See http://bugs.eclipse.org/bugs/show_bug.cgi?id=263154#c3.
Comment 3 Anton G. CLA 2011-04-26 03:16:36 EDT
(In reply to comment #2)
> It would be nice if the logic implemented in this checker was also applied to
> content assist. See http://bugs.eclipse.org/bugs/show_bug.cgi?id=263154#c3.

I think, CODAN warning (or error) will be quite enough. I want context assist to show class members cause if I made method static by mistake I want to finish it first and then fix its signature.

However, I can think about moving checking functionality to CDT core utils. Is it necessary?
Comment 4 Sergey Prigogin CLA 2011-04-26 10:47:26 EDT
(In reply to comment #3)
> However, I can think about moving checking functionality to CDT core utils. Is
> it necessary?

Yes, please.
Comment 5 Elena Laskavaia CLA 2011-04-26 21:01:27 EDT
Note that 3 and 4 are semantic error (i.e. would be caught by a compiler) and as such should be of diffrent category (done) and marker type (markerType=org.eclipse.cdt.codan.core.codanSemanticProblem), and well as run as you type only (not on full build because there is no point compete with compiler)
Comment 6 Anton G. CLA 2011-04-27 06:19:22 EDT
(In reply to comment #5)
> Note that 3 and 4 are semantic error (i.e. would be caught by a compiler) and
> as such should be of diffrent category (done) and marker type
> (markerType=org.eclipse.cdt.codan.core.codanSemanticProblem), and well as run
> as you type only (not on full build because there is no point compete with
> compiler)

Thanks, I fixed marker type, but I cannot understand how I should disable checker on full build. Can you help me?
Comment 7 Elena Laskavaia CLA 2011-05-01 20:42:32 EDT
> 
> Thanks, I fixed marker type, but I cannot understand how I should disable
> checker on full build. Can you help me?

	@Override
	public void initPreferences(IProblemWorkingCopy problem) {
		super.initPreferences(problem);
		// these checkers should not run on full or incremental build
		getLaunchModePreference(problem).enableInLaunchModes(CheckerLaunchMode.RUN_AS_YOU_TYPE, CheckerLaunchMode.RUN_ON_DEMAND);
	}
Comment 8 Anton G. CLA 2011-05-02 04:01:56 EDT
(In reply to comment #7)
>     @Override
>     public void initPreferences(IProblemWorkingCopy problem) {
>     }

Thanks!
Comment 9 Anton G. CLA 2011-05-02 04:46:22 EDT
Created attachment 194464 [details]
Fixed checker implementation (see comment for details)

Fixed:
 - fixed marker types;
 - run as you type only;
 - tests were added to integration suite.
Comment 10 Eclipse Genie CLA 2019-03-23 10:57:01 EDT
New Gerrit change created: https://git.eclipse.org/r/139367
Comment 11 Eclipse Genie CLA 2019-09-28 03:08:02 EDT
New Gerrit change created: https://git.eclipse.org/r/150293