I've already got an answer for Add parentheses if double superscript. I want to go a step further as proposed below.
However lualatex complains of “missing $” when using the e{_}-type argument…
\documentclass[12pt, a4paper, twoside]{report}
\RequirePackage{xparse}
\ExplSyntaxOn
\cs_generate_variant:Nn \tl_if_in:nnTF { xnTF }
\bool_new:N \l_tmp_ind_a_bool
\bool_new:N \l_tmp_ind_b_bool
\bool_new:N \l_tmp_exp_a_bool
\bool_new:N \l_tmp_exp_b_bool
\NewDocumentCommand{\ajoutParentheses}{me{_}e{^}}{%
\tl_if_in:xnTF { #1 } { _ }{ \bool_set_true:N \l_tmp_ind_a_bool }{ \bool_set_false:N \l_tmp_ind_a_bool }
\IfValueTF{ #2 }{ \bool_set_true:N \l_tmp_ind_b_bool }{ \bool_set_false:N \l_tmp_ind_b_bool }
\tl_if_in:xnTF { #1 } { ^ }{ \bool_set_true:N \l_tmp_exp_a_bool }{ \bool_set_false:N \l_tmp_exp_a_bool }
\IfValueTF{ #3 }{ \bool_set_true:N \l_tmp_exp_b_bool }{ \bool_set_false:N \l_tmp_exp_b_bool }
\ensuremath{%
\bool_if:nTF { (\l_tmp_ind_a_bool && \l_tmp_ind_b_bool) || (\l_tmp_exp_a_bool && \l_tmp_exp_b_bool) } { \left(#1\right) } { #1 }\IfValueT{ #2 }{ _{#2} }\IfValueT{ #3 }{ ^{#3} }%
}%
}
\ExplSyntaxOff
\begin{document}
\section{Works}
\ajoutParentheses{b}
\ajoutParentheses{b}^2
\ajoutParentheses{b_0}
\ajoutParentheses{b_0}^2
\ajoutParentheses{b^0}
\ajoutParentheses{b^0}^2
\section{Doesn't work}
\ajoutParentheses{b}_1
\ajoutParentheses{b}_1^2
\ajoutParentheses{b_0}_1
\ajoutParentheses{b_0}_1^2
\ajoutParentheses{b^0}_1
\ajoutParentheses{b^0}_1^2
\end{document}

\ensuremath: the only modification I suggest is the use of the pair\left( \right). – NBur Mar 28 '19 at 12:56\leftand\rightin my answers. – egreg Mar 28 '19 at 13:03