[PATCH] Add new warning to Clang to detect when all code paths in a function has a call back to the function.

Richard Smith richard at metafoo.co.uk
Tue Nov 12 22:10:37 PST 2013



================
Comment at: lib/Sema/AnalysisBasedWarnings.cpp:108-109
@@ +107,4 @@
+      if (CE && CE->getCalleeDecl() == FD) {
+        if (const CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(CE)) {
+          if (isa<CXXThisExpr>(MCE->getImplicitObjectArgument())) {
+            state = AfterFunctionCall;
----------------
If the function is non-virtual, you should warn even if the object argument is different.

================
Comment at: lib/Sema/AnalysisBasedWarnings.cpp:107
@@ +106,3 @@
+      const CallExpr *CE = dyn_cast<CallExpr>(I->getAs<CFGStmt>()->getStmt());
+      if (CE && CE->getCalleeDecl() == FD) {
+        if (const CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(CE)) {
----------------
Compare canonical declarations here.

================
Comment at: lib/Sema/AnalysisBasedWarnings.cpp:85-87
@@ +84,5 @@
+enum RecursiveState {
+  Unknown,
+  AfterFunctionCall,
+  Reachable
+};
----------------
These names are a bit confusing. They seem to mean:

Unknown: no paths to here have been visited
AfterFunctionCall: no paths to here have been found that don't recurse
Reachable: paths to here have been found that don't recurse

How about `FoundNoPath`, `FoundPath`, `FoundPathWithNoRecursiveCall` or similar?

================
Comment at: lib/Sema/AnalysisBasedWarnings.cpp:96-97
@@ +95,4 @@
+
+  if (states[ID] < state)
+    states[ID] = state;
+  else
----------------
Once you set the exit block to `Reachable`, you can bail out of the traversal early.


http://llvm-reviews.chandlerc.com/D1864



More information about the cfe-commits mailing list