I'm using the code below to get a todolist of all the comments I have in my document. The problem is the list displays the last section of the document, but it hasn't any comments. Only sections with comments should be displayed. Any Idea how to solve this??
\documentclass{article}
\usepackage{todonotes,tocloft,xpatch,hyperref}
% This is based on classicthesis chapter definition
\let\oldsec=\section
\renewcommand*{\section}{\secdef{\Sec}{\SecS}}
\newcommand\SecS[1]{\oldsec*{#1}}%
\newcommand\Sec[2][]{\oldsec[\texorpdfstring{#1}{#1}]{#2}}%
% http://tex.stackexchange.com/a/61267/11984
\makeatletter
\xapptocmd{\Sec}{\addtocontents{tdo}{\protect\todoline{\thesection}{#1}{}}}{}{}
\newcommand{\todoline}[1]{\@ifnextchar\Endoftdo{}{\@todoline{#1}}}
\newcommand{\@todoline}[3]{%
\@ifnextchar\todoline
{}
{\contentsline{section}{\numberline{#1}#2}{#3}{}{}}%
}
\let\l@todo\l@subsection
\newcommand{\Endoftdo}{}
\AtEndDocument{\addtocontents{tdo}{\string\Endoftdo}}
\makeatother
\begin{document}
\listoftodos
\tableofcontents
\section{My first Section}\todo{Section 1 note}
\subsection{A first Subsection}
\subsubsection{A first subsubsection}\todo{Section 1, section 1, subsection note}
\subsection{Another subsection}\todo{Section 1, section 1, subsection note 2}
\section{My second Section}
\subsection{Another subsection}\todo{Section 2, subsection 1 note}
\subsection{Another subsection}\todo{Section 2, subsection 2 note}
\section{My third Section}
\section{My fourth Section}
\end{document}