Community
Participate
Working Groups
Given the following code: struct S { void f(); }; void g() { S array[5]; for (auto& s : array) s.f(); // ERROR HERE } the parser gives an error: f: method could not be resolved It works fine if I write in the type explicitly, as in: for (S& s : array) s.f(); I wasn't sure whether this belongs under Codan or Indexer - please reassign as appropriate.
Created attachment 185768 [details] testcase + fix The patch fixes the issue for array-types. However, there is also a mechanism for non-array types that needs to be implemented. -> Fixed for array-types in 8.0 > 20101223.
*** cdt cvs genie on behalf of mschorn *** Bug 332883: auto-type in range-based for. [*] CPPVisitor.java 1.145 http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java?root=Tools_Project&r1=1.144&r2=1.145 [*] AST2CPPTests.java 1.376 http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.cdt-core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java?root=Tools_Project&r1=1.375&r2=1.376
Created attachment 190148 [details] testcase + fix for the case where the initializer is not an array
Fixed in 8.0 > 20110302
*** cdt cvs genie on behalf of mschorn *** Bug 332883: Range based for loop with non-array type. [*] CPPVisitor.java 1.150 http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java?root=Tools_Project&r1=1.149&r2=1.150 [*] CPPSemantics.java 1.201 http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPSemantics.java?root=Tools_Project&r1=1.200&r2=1.201 [*] AST2TemplateTests.java 1.209 http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.cdt-core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2TemplateTests.java?root=Tools_Project&r1=1.208&r2=1.209 [*] AST2CPPTests.java 1.381 http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.cdt-core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java?root=Tools_Project&r1=1.380&r2=1.381
While it now works correctly for the testcase you added in AST2CPPTests.java, I still get an error for the following: #include <vector> struct S { void f(); }; void g() { std::vector<S> vec[5]; for (auto s : vec) s.f(); // ERROR HERE: Method 'f' could not be resolved }
Sorry, there should be be no "[5]" in that last example. (It gaves the same error without the "[5]".)
Update: it seems to be a preprocessing issue since it works fine if I replace "#include <vector>" with the preprocessed source code of <vector>. There is one other small issue: in the code below, if I use "auto" as the type of "s", the "f()" in "s.f()" is syntax-highlighted differently and I cannot jump to f()'s declaration using "Open Declaration". If I replace the "auto" with "S", I can now jump to the declaration using "Open Declaration". #include <vector> struct S { void f(); }; void g() { std::vector<S> vec; for (auto s : vec) s.f(); }
With regards to the "preprocessing issue": it appears to be due to https://bugs.eclipse.org/bugs/show_bug.cgi?id=338611, since std::begin uses decltype to declare its return type. Just as that issue goes away if I inline the contents of the header file where the function is declared into the file where it's used, so the issue here goes away if I inline the contents of <vector> into my source file. I am still *very* curious to know why there is this difference in behaviour between using a header file and copy-pasting its contents into the source file directly.
(In reply to comment #9) > With regards to the "preprocessing issue": it appears to be due to > https://bugs.eclipse.org/bugs/show_bug.cgi?id=338611, since std::begin uses > decltype to declare its return type. Just as that issue goes away if I inline > the contents of the header file where the function is declared into the file > where it's used, so the issue here goes away if I inline the contents of > <vector> into my source file. There may be many reasons, why we cannot correctly index <vector> when the header is using c++0x. Nevertheless, the range-based for loop is implemented correctly. > I am still *very* curious to know why there is this difference in behaviour > between using a header file and copy-pasting its contents into the source file > directly. Declarations that are found in the header are stored in the index and can subsequently be used by the parser (without looking at the header again). Especially in the area of dependent names, types and expressions the reperesentation in the index is less complete than the representation directly built by the parser.
This still does not seem to be working properly in some cases. For example: test.hpp: namespace std { template <class T1, class T2> struct pair { T1 first; T2 second; }; template <typename T, typename U> T begin(const pair<T, U>& p) { return p.first; } template <typename T, typename U> U end(const pair<T, U>& p) { return p.second; } } test.cpp: #include "test.hpp" struct S { int x; }; int main() { S arr[5]; std::pair<S*, S*> p{arr, arr + 5}; for (const auto& r : p) r.x; // ERROR HERE: Field 'x' could not be resolved } Interestingly, if I try this with a type in some namespace other than std, it works. Does the parser/indexer somehow treat namespace std specially?
Thanks for the example! Added testcase and fix.
*** cdt git genie on behalf of Markus Schorn *** Bug 332883: Auto type for range based for loop. [*] http://git.eclipse.org/c/cdt/org.eclipse.cdt.git/commit/?id=35a66ea7636bcf66e8c64c304ad0733e6b565172
*** cdt git genie on behalf of Markus Schorn *** Bug 332883: Auto type for range based for loop. [*] http://git.eclipse.org/c/cdt/org.eclipse.cdt.git/commit/?id=f492f351361ec3ad62cb9691fde2532a0dca5c47